Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

node_modules directory location #775

Closed
lukaszsagol opened this Issue · 18 comments
@lukaszsagol

For convenience, user should be able to change node_modules directory location via npm config set root DIR.

@aarek

+1

@isaacs
Owner

npm config set prefix?

If you want to spell "node_modules" differently, sorry, no can do. That's built into node-core. Changing that is tantamount to building a completely different package manager.

@isaacs isaacs closed this
@gametbt

Is it possible that you don't change the container dir (node_modules), but you could set the
parent dir to something like: npm config set prefix /opt/www/lib/node_modules
or use a different config option: npm config set node_modules /opt/www/lib/node_modules
Of course, this need not be forced, if the above option is set modules get installed there,
if not, as is done now, local install will be chosen.

@isaacs
Owner

This is not going to change any time soon.

@rjduwors

I am stumbling painfully through first time installation issues, having manage to build nodejs and curl in npm. But.. node is looking for its modules in /usr/local/lib/node while npm is dumping them into /usr/local/lib/node_modules. At first node.io could not run any job from the file system, although the component examples worked. A little bit of -g, and nodejs at the top of the stack could not find node.io...until I copied the contents of lib/node_modules into lib/node. It would appear this is a disconnect between how the nodejs and npm are being configured. Any suggestions to clarify will be appreciated.

@mdesantis

I would appreciate this feature too... I have that node_modules directory that make me feel my $HOME dirty

@isaacs
Owner

wontfix. Probably not ever. Here's why:

  1. Global modules are not for require(). Period. That is a specific design choice which was very carefully considered based on the tradeoffs in complexity, maintenance, bug-causing (which comes back to npm, and thus my todo list, more often than one would probably expect.) Global modules are for command line programs that you want to put in your $PATH environment variable. If you hack your NODE_PATH environ to include {prefix}/lib/node_modules, fine, then it's on you to deal with any issues. But this must be an intentional step. I want it to feel like you're making a significant non-standard choice, because you are.

  2. If you want modules to be in /opt/www/lib/node_modules, then just npm config set prefix /opt/www. It'll also be on you to add /opt/www/bin to your PATH, and /opt/www/share/man to your MANPATH. npm help folders

  3. If you don't want to install modules locally, you don't have to. Just add the -g flag to the command. This is how you can add binaries to your path.

  4. If you don't want to install modules into ~/node_modules, you don't have to. Delete that whole directory if you like. cd into a project you're working on, throw a package.json in there, and run npm install to install all your deps. It's quite easy. The local-install approach is why you can just delete that directory and be done with it, because npm doesn't put anything outside of that directory (except in the case of global installs, which is part of why they kinda suck to maintain.)

  5. If you want to have a single install place which is shared by multiple modules, or you want to install something globally (to get the command line utils) and also locally (to get the require()-able modules), then use the npm link command. It's documented thoroughly in npm help link.

So, to recap:

  • Every use case is already covered.
  • Making the suggested changes would make everything worse.
  • Been there, done that, that's how I know how much it sucks.
@mdesantis

If you want modules to be in /opt/www/lib/node_modules, then just npm config set prefix /opt/www. It'll also be on you to add /opt/www/bin to your PATH, and /opt/www/share/man to your MANPATH. npm help folders

Does it mean that if I do

npm config set prefix /opt/
npm install some-lib

I should see some-lib installed under /opt/node_modules ? Here it doesn't work, npm ignores the setting and install some-lib in ~/node_modules. Am I missing something?

@isaacs
Owner

No, because only global installs go into the global location. Local installs go to ./node_modules.

Also, it'd be /opt/lib/node_modules. npm doesn't put things directly in the prefix.

npm help folders for more info.

@mdesantis

Ok, I got it. Thank you

@ciscoo ciscoo referenced this issue from a commit in ciscoo/jeet
@ciscoo ciscoo Added devDependencies for Grunt dfc30d3
@feeela

@isaacs “Every use case is already covered.”

Nope, you left out a clean root directory to support clarity for the whole project.

I guess you are just thinking about node and npm, but in reality JS is not the only programming language and each language has its own package manager. If every package manager would pollute the root directory, the project looks like a mess. Think about combining Composer for PHP packages with npm for JS modules, git for versioning the code and Ant or Grunt to build and deploy all that through Jenkins continuous integration. This is a more realistic example, which still is simplified. Most of those tools are configurable so that I could throw the configuration files into a subdirectory "etc/deploy" (or something like that). I don't think that you will change your mind, but I see a clear benefit here and felt the urge to show you that view.

@indus

@feeela: totally agree!

@danschumann

This seems to be covered by NODE_PATH=/something/node_modules

lets say you have /media/sf_shared and you can't install symlinks in there, which means you can't actually npm install from /media/sf_shared/myproject because some modules use symlinks.

  • $ mkdir /home/dan/myproject && cd /home/dan/myproject
  • $ ln -s /media/sf_shared/myproject/package.json you can symlink in this direction, just can't create one inside of /media/sf_shared
  • $ npm install
  • $ cd /media/sf_shared/myproject
  • $ NODE_PATH=/home/dan/myproject/node_modules node index.js

That's how I got my situation figured out. I thought I'd leave this here since this comes up in google for this issue and there's not much else.

An alternative might be ( but more typing in the long run )

  • $ cd /media/sf_shared/myproject
  • $ mkdir /home/dan/myproject && npm install --prefix /home/dan/myproject
  • $ NODE_PATH=/home/dan/myproject/node_modules node index.js

The 2nd way is more typing because everytime you install, you have to type the whole prefix thing, whereas if the package.json is symlinked, then you can just have a tmux window you switch to and do npm install. Either way its a little clunky, but avoiding symlinks I guess would be.

@deifyed

+1 on the basis of feeela's reasoning.

@VladShcherbin

this is ridiculous stupid that I can't normally specify the node_modules path to e.g. vendor/node_modules. With bower I can easily specify the directory in .bowerrc, WHY can't it be so simple with node ?
Is it so hard to be people oriented ?

@avdd

@KenanY That FAQ is about what the folder is called, not where it is located. We want to locate the node_modules folder, with the same name, somewhere else. Node supports this with NODE_PATH.

Maybe --global and --prefix is the right way but the behaviour is different (e.g. lib/node_modules) and some tools (at least grunt) don't seem to support it.

@othiym23 othiym23 locked and limited conversation to collaborators
@othiym23
Owner

Locking this because this is settled business, and bikeshedding it here is unlikely to ever be fruitful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Something went wrong with that request. Please try again.