11

Recent node docs say that modifying require.paths is bad practice. What should I do instead?

4 Answers 4

5

I believe the concern is that it can be repeatedly modified at run time, rather than just set. That could obviously be confusing and causes some quite bizarre bugs. Also, if individual packages modify the path the results are applied globally, which is really bad and goes against the modular nature of node.

If you have several library paths of your own, the best solution is to set the NODE_PATH environment variable before launching node. Node then picks this up when it's launched and applies it automatically.

Sign up to request clarification or add additional context in comments.

3 Comments

that will require an additional external script to just run the app which is kind of unclean.
Just in case anyone still stumbles across this answer, the matter is now a little academic given how node & npm have evolved towards a sandboxed development.
@leebriggs Can you explain your last comment a bit please?
1

I keep the related models in the same dir or a sub dir and load using:

var x = require('./mod/x');

In case it's an external module, I install it using npm that puts the module correctly in NODE_PATH.

I've never changed require.paths.

3 Comments

For my use case, I want to keep local copies of specific (possibly patched) versions of dependencies in a local deps/ folder. Perhaps amending NODE_PATH would be the right way to do it, but that means writing a launcher script...
Can't you require a module using a relative path like var x = require('../deps/c'); ? I never did that but saw somewhere.
@nornagon I know this is old but I have found the best solution is to add symlinks to node_modules pointing to each module in the deps/ directory. Then you can reference them as if they were normal modules. Make sure to update your .gitignore for each node_modules symlink you add so that the symlinks are checked into source control. It's hacky, but it works.
1

have a look at https://github.com/patrick-steele-idem/app-module-path-node; you can add a directory to the require statements in the top level, without influencing the paths of sub-modules.

2 Comments

I've been using this module for a while. It works really well.
If you just need to do a quick hack, you can simply wrap require ('module').Module._nodeModulePaths in another function. Remember though that changing the module path in this manner is still bad practice.
0

Unless I'm making a mistake in my understanding, the primary limitation of the current system is that for namespacing you're stuck without the uses of folders for non-hierarchical dependencies.

What that means in practice...

Consider that you have x/y/z and a/b as well as a/b/c. If both a/b and a/b/c depend on z/y/z you end up having to either specify that relatively (require('../../x/y/z') and require('../../../x/y/z') respectively) or having to make every single package a node_module. Failing that you can probably do horrific things with symlinks or similar.

As far as I can tell the only alternative is to rather than use folders to namespace and organise, use filenames such as:

  • a.b.js
  • a.b.c.js
  • x.y.z.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.