Live data from Hacker News

How I Work Around The require(“../../../../../../../”) Problem In NodeJS

lostechies.com

21–30 of 44 posts

Re: How I Work Around The require(“../../../../../../../”) Problem In NodeJS

#23
Anyone with this problem is most likely working on a horrible monolith. You have far too much nesting and you're simply hiding the symptoms of a greater problem. Flat > nested. Simple > complex. Law of demeter also applies to reaching and in/out of folders. Break your app into more smaller modules rather than sweeping your mess under the rug.

Re: How I Work Around The require(“../../../../../../../”) Problem In NodeJS

#24
post #15

For me, a relative path in a require statement signifies that the required code is a part of the application itself. Consider: require('foo'); Versus: require('./foo'); If I find that a module is becoming popular, I may turn it into an npm module by moving it to it's own folder and adding a package.json, then using npm's bundledDependencies property to inform npm that it lives locally. (edit: Note that I've shifted m…

The double dots refer to the parent directory; I'm not sure that there is other associated terminology or if there is an easier way to express what you wrote.

Re: How I Work Around The require(“../../../../../../../”) Problem In NodeJS

#26
post #15

For me, a relative path in a require statement signifies that the required code is a part of the application itself. Consider: require('foo'); Versus: require('./foo'); If I find that a module is becoming popular, I may turn it into an npm module by moving it to it's own folder and adding a package.json, then using npm's bundledDependencies property to inform npm that it lives locally. (edit: Note that I've shifted m…

If find it's easier to distinguish modules from files by requiring modules like:

  require('foo');
and application files like:

  require('./foo.js'); //.js extension

Re: How I Work Around The require(“../../../../../../../”) Problem In NodeJS

#27
post #12

can't you install a private npm module by npm link? That won't touch the npm servers [1]. Then your other project in the same file system can just require like a first class public npm module. [1] https://www.npmjs.org/doc/cli/npm-link.html

I don't think npm link is meant to be used in production like that.

You probably shouldn't be using npm for production builds. npm is good for libraries, but applications should probably be tarballed.

Re: How I Work Around The require(“../../../../../../../”) Problem In NodeJS

#30
I've been using the AngularJS dependency injection module for all my application code, [1] which means I am now only requiring libraries. I find it makes for much cleaner code and easier to test as well.

[1] https://github.com/FungusHumungus/pongular

Post reply on HN