Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

doc: adding note on loading arbitrary paths from node_module #7794

Closed
wants to merge 1 commit into from
Closed

doc: adding note on loading arbitrary paths from node_module #7794

wants to merge 1 commit into from

Conversation

sudodoki
Copy link

Adding note to api/modules.markdown about ability to load arbitrary path from modules from node_module path by adding reference to path after module name in call to require(). Possible fix for #7787.

@trevnorris trevnorris added the doc label Jun 25, 2014
@sudodoki
Copy link
Author

sudodoki commented Aug 6, 2014

Anything I should edit/add/change in this?

@indutny
Copy link
Member

indutny commented Aug 6, 2014

Heya, sorry for missing this. We usually wrap lines at 80 column in all documentation and code. Could you please fix this?

Regarding the change itself, I'd rather ask @tjfontaine to take a look at it.

@sudodoki
Copy link
Author

sudodoki commented Aug 6, 2014

We usually wrap lines at 80 column in all documentation and code. Could you please fix this?

Sure, will repush this one later today.

@sudodoki
Copy link
Author

sudodoki commented Aug 6, 2014

Did update note, honoring the 80 chars limit.

@trevnorris
Copy link

@tjfontaine ping

@tjfontaine
Copy link

So what you're trying to document is that you can require "into" modules? The phrasing here could be better, we don't really want to say "arbitrary" in quite the same connotation. Does that make sense?

@sudodoki
Copy link
Author

Should it read like

There's also a possibility to require into module by specifying path after 
module name in call to `require()`. Sample call would be 
`require('module-name/and/path/to/file')`, which would go through all default 
`node_modules` locations, looking for specified path (`'and/path/to/file'`) to 
load.

?

@tjfontaine
Copy link

How about

You can require specific files or sub modules distributed with a module by including a path suffix after the module name. For instance require('example-module/path/to/file') would resolve path/to/file relative to where example-module is located. The suffixed path follows the same module resolution semantics.

Adding note to api/modules.markdown about ability to load arbitrary path from modules from node_module path by adding reference to path after module name in call to require()
@sudodoki
Copy link
Author

Changed text to what @tjfontaine said.

@sudodoki
Copy link
Author

There's still a thing that isn't told: if you are missing the file in the original module required it would traverse all the other possible locations and look for while.
Say you have tree structure like:

.
|-- node_modules
|   `-- test
|       `-- arbitrary.js
`-- test_require_subfolder
    |-- node_modules
    |   `-- test
    |       `-- index.js
    `-- test-require.js

And in test-require.js you have

console.log(require('test/arbitrary'))

It would still go and get arbitrary.js from test from parent's folder node_modules.
Not sure if I was able to explain issue properly & whether it's not obvious from the original statement by tjfontaine.

@markstos
Copy link

markstos commented Nov 6, 2014

I ran into this lack of documentation today and found this issue report. I've reviewed the docs and found them to be improvement. Merging is recommended.

@trevnorris
Copy link

@chrisdickinson LGTM. Feel free to merge.

chrisdickinson pushed a commit that referenced this pull request Nov 10, 2014
Adding note to api/modules.markdown about ability to
load arbitrary path from modules from node_module path
by adding reference to path after module name in call
to require()

PR-URL: #7794
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
Reviewed-by: Fedor Indutny <fedor@indutny.com>
Reviewed-by: Timothy J Fontaine <tjfontaine@gmail.com>
@chrisdickinson
Copy link

Merged in fd22268. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants