Reputation:
Per several github docs, I have tried this, although it is really for the require keyword, as the very first line in the first server file fired [index.js]:
process.env.NODE_PATH = 'app';
require('module').Module._initPaths();
It didn't work. Then I read up in webpack to do this, but still it does not work:
resolve: {
root: path.resolve(path.join(__dirname + '/app')),
alias: {
app: 'app',
component: 'app/components',
utility: 'app/utils'
},
extensions: ['', '.js', '.jsx']
},
How do I avoid the endless '../../../../' as my project goes deeper as I do not want to keep doing this:
Import MyComponent from '../../../../../Whatever' ?
Thanks
Upvotes: 2
Views: 1632
Reputation: 462
Currently I am using quite the same solution, no 6 in this list.
process.env.NODE_PATH = __dirname;
require('module').Module._initPaths();
Which enables me to call all my js files by folder/filename wherever I am in my code base, e.g.
require('services/userService');
This is a quite ugly solution since it relies on a private Node.js core method, this is also a hack that might stop working on the previous or next version of node.
I also asked the same question on Quora with some answers.
Upvotes: 2