Live data from Hacker News

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

lostechies.com

31–40 of 44 posts

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

#33

Setting an environment variable to include a directory in which you have libraries that you need to use across your application is not a workaround. It's using a feature.

So? Asking earnestly, what exactly is your point? That the title offends you because it uses the phrase "work around"?

Adding a relative path to a *PATH variable is clever, elegant, useful, and uncommon. It resolves a problem not directly (by changing how 'require' works internally), but indirectly, aka "working around" it.

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

#35
Your project is horribly architected if you need to do relative requires this deep.

Applications can be decoupled into interoperable components. Separate modules for configuration, controllers, routing, etc.

Separate concerns into modules.

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

#36

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

How do you go about setting these links when you freshly checked out a project through source control? There is no "npm link all", is there?

npm scripts on install or prepublish

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

#37
I work around this problem in a couple projects by putting my code in modular directories in ./src/node_modules. no npm magic, no having to make symlinks or change path, just plain node. it also makes it very easy to publish your modules when you are ready, just take the module directory out, give it a package.json, npm publish the module, npm install --save the module back in the project.

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

#38
post #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.

+100 — NODE_PATH actually ends up being an anti-pattern because all of those internal modules are still not versionable. Instead, use a private NPM registry and just publish everything separately.

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

#40
Am I the only one that doesn't find this a problem?

My usual idiom is to do two things:

1. Part of my boilerplate at the top of every JS file is something like this:

    var path     = require('path');
    var HOMEDIR  = path.join(__dirname,'..','..');
where `__dirname` is the built-in variable that names the directory that contains the current file, and `'..','..'` is the requisite number of steps up the directory tree to reach the root of the project.

From there is it is simply:

    var foo = require(path.join(HOMEDIR,'lib','foo'));
    var bar = require(path.join(HOMEDIR,'lib','foo','bar'));
to load an arbitrary file within the project.

2. Long before I got to 8 levels deep in the directory tree I'd create a separate npm module to bundle the code together in a less spaghetti fashion.

For what it's worth, I typically use a branch on a private GitHub repository for my "private" modules. I.e., the master branch has the source code, and another (say, `npm-v1.2.3`) has the npm package of it.

Post reply on HN