Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could this be in a setup file? #6

Closed
ariporad opened this issue Sep 8, 2015 · 5 comments
Closed

Could this be in a setup file? #6

ariporad opened this issue Sep 8, 2015 · 5 comments

Comments

@ariporad
Copy link
Contributor

ariporad commented Sep 8, 2015

Hi,

I really like your module, and have used it in the past, but I have a quick question for my current project.

I'd like to have a setup.js file which looks like this:

require('babel/polyfill');
require('app-module-path').addPath(__dirname);
require('dotenv').load();

Then in my index.js file:

require('./setup');
// something something something. This is a really cool file
var http = require('http')
// ...

Would that work?

@philidem
Copy link
Collaborator

philidem commented Sep 8, 2015

@ariporad I'm almost certain that the line require('app-module-path').addPath(__dirname); still needs to be in the entry point of your NodeJS application due to how the app-module-path module hooks into the module loader.

If I have multiple entry points then add that line to each entry point file. For example if your application can be ran via node server.js and node build.js then add that line to server.js and build.js.

If you know of a way to modify app-module-path so that it can add a search path directory while in a non-entry point file then please let us know. Otherwise, I think we can close this issue with the answer that putting require('app-module-path').addPath(__dirname); in setup.js in your example will not work. You will need to add that line to index.js.

@ariporad
Copy link
Contributor Author

ariporad commented Sep 8, 2015

I'm working on a test for it, I'll let you know tomorrow morning.

Thanks!Ari

On Mon, Sep 7, 2015 at 7:58 PM, Phillip Gates-Idem
notifications@github.com wrote:

@ariporad I'm almost certain that the line require('app-module-path').addPath(__dirname); still needs to be in the entry point of your NodeJS application due to how the app-module-path module hooks into the module loader.
If I have multiple entry points then add that line to each entry point file. For example if your application can be ran via node server.js and node build.js then add that line to server.js and build.js.

If you know of a way to modify app-module-path so that it can add a search path directory while in a non-entry point file then please let us know. Otherwise, I think we can close this issue with the answer that putting require('app-module-path').addPath(__dirname); in setup.js in your example will not work. You will need to add that line to index.js.

Reply to this email directly or view it on GitHub:
#6 (comment)

@ariporad
Copy link
Contributor Author

ariporad commented Sep 8, 2015

I just tested it, it appears to work. You can see the results here: https://github.com/ariporad/test-app-module-path-in-setup-file

@philidem
Copy link
Collaborator

philidem commented Sep 8, 2015

@ariporad maybe I'm missing something but your test project doesn't have a setup.js file. The line that uses app-module-path is in src/index.js which I assume is the entry point. Is this what you intended for your test?

@patrick-steele-idem
Copy link
Owner

I would actually expect it to work since app-module-path walks up the require tree to update the paths for already loaded parent modules using the following code:

while(parent && parent !== require.main) {
addPathHelper(parent.paths);
parent = parent.parent;
}

That was a later code change, but it is something that is supported. So to answer your question: Yes, it can be in a setup.js file.

I'm going to go ahead and close this issue, but feel free to comment or open another issue if you have other questions or concerns. Thanks.

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

No branches or pull requests

3 participants