npm won't install packages locally. What's wrong?

user1449536 picture user1449536 · Jun 11, 2012 · Viewed 27k times · Source

I want to install packages locally, but npm is always installing packages to the global location. I'm running the following command:

npm install serialport

I do not have a .npmrc command and I'm not using the -g flag, so I don't know why it's not installing locally. Here's a snippet from the config dump showing that global is false: $ npm config ls -l | grep global global = false globalconfig = "/usr/local/etc/npmrc" globalignorefile = "/usr/local/etc/npmignore"

And the packages are still being installed like this

[email protected] ../../../../node_modules/serialport

So unless I am totally wrong about what "local" means, this seems wrong. I was under the impression that "local" meant in the current working directory so that I could do a "require" in my main code file. See: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/ as referenced in a previous npm related question.

Can someone please give me some hints on this? Thank you very much.

P.S. It's not specific to the serialport module. It's happening with all of them.

Answer

Pickels picture Pickels · Jun 11, 2012

Most of my answer can be found: http://npmjs.org/doc/folders.html#More-Information

What I understand is that npm will try to install it in a sensible location. So if you have a project/node_modules directory and you are in /project and do npm install it will use product/node_modules.

Now if you accidentally did a cd project/css and do npm install then npm will traverse up until it finds your node_modules directory. This is to prevent you from accidentally installing it in your project/css.

So in your case you have a node_module directory somewhere in the path of your project. So my guess is that you can fix it by either deleting that directory or manually creating a node_modules dir in your project folder.