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

30,494

Solution 1

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.

Solution 2

If you have a package.json file on the folder you are trying to install the package, then it will create the node_modules folder correctly.

Basic package.json

{
    "name": "application-name",
    "version": "0.0.1"
}
Share:
30,494
user1449536
Author by

user1449536

Updated on July 09, 2022

Comments

  • user1449536
    user1449536 almost 2 years

    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.

  • user1449536
    user1449536 almost 12 years
    Yeah, I just found that myself and tried to answer my own question, but new users can't do that until 8 hours later. Here's what I found that explained it. stackoverflow.com/questions/6514621/npm-install-locally
  • Pickels
    Pickels almost 12 years
    No worries, I actually wasn't 100% sure until I found that documentation page.
  • idmean
    idmean about 10 years
    Your answer is nearly correct, but this folder is called node_module s