简体   繁体   中英

Globally installed packages are still being installed locally

I'm using node.js tools for my development environment only (ie devDependencies, nothing in production) - such as grunt, uglify, sass, bower etc.

I manage a lot of projects, and each one is using the exact same tools. Further, I move these projects around often, including zipping them up, putting some in Dropbox, etc.

Often, the size of the node_modules folder in each project is becoming a bug bear - my PC sometimes freezes while deleting it, archives end up massive before I realise I forgot to delete node_modules first, etc. etc.

As I understood it, I could install node packages globally (using npm install -g <package> ) and if the right package was available globally, npm wouldn't need to install it again in each of my projects' npm_modules folder when I run npm install .

Have I understood this correctly?

If so, this doesn't appear to be working for me:

  • I ran npm install -g bower
  • cd into project folder and npm install
  • The node_modules/bower folder is created in all its glory

In my package.json, my bower dependency reads like this:

"devDependencies": {
  "bower": "^1.7.7",

I tried setting this to "*" and repeating the above steps, in case npm was trying to ensure only a certain version was available, but I got the same result.

I understand that the point of managing dependencies is so different versions of a package can be in use across different projects; in my case because I'm only using devDependencies I'm quite happy to use the same/latest version of everything, because I know it all works once it's deployed anyway.

As you can probably tell, I'm fairly new to node.js. Have I understood properly how this is meant to work? Or am I going about it the wrong way?

In case it is relevant, I'm on Windows 10 and am using Git Bash (which appears to be based on MinGW).

In my opinion you are doing things the correct way. I tend to shy away from installing npm modules globally. As you mention, this makes your projects very portable. All of your dependencies are captured in the local directory. You can zip up the whole thing, put it on Dropbox or wherever and you know it has everything.

I'm surprised your project directories including node_modules are so large to the point you are noticing performance problems. If that is actually the case, I would double check there aren't any unused dependencies in your package.json. If you are worried that there are deps installed in your project that aren't in your package.json (installed ad hoc), you can delete your node_modules directory and rerun npm install . You always want package.json to be an accurate list of your project's deps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM