简体   繁体   English

全局安装的软件包仍在本地安装

[英]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. 我只在我的开发环境中使用node.js工具(即devDependencies,生产中没有任何东西) - 例如grunt,uglify,sass,bower等。

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. 此外,我经常移动这些项目,包括压缩它们,将一些项目放入Dropbox等。

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. 通常,每个项目中node_modules文件夹的大小正在成为一个bug - 我的PC有时会在删除它时冻结,在我意识到我忘记首先删除node_modules等之前,档案最终会大量存档。

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 . 据我所知,我可以全局安装节点包(使用npm install -g <package> ),如果正确的包全局可用,当我运行时,npm不需要在我的每个项目的npm_modules文件夹中再次安装它npm install

Have I understood this correctly? 我理解正确吗?

If so, this doesn't appear to be working for me: 如果是这样,这对我来说似乎不起作用:

  • I ran npm install -g bower 我跑了npm install -g bower
  • cd into project folder and npm install cd到项目文件夹和npm install
  • The node_modules/bower folder is created in all its glory node_modules / bower文件夹创建了它的所有荣耀

In my package.json, my bower dependency reads like this: 在我的package.json中,我的bower依赖关系如下所示:

"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. 我尝试将其设置为“*”并重复上述步骤,以防npm尝试确保只有某个版本可用,但我得到了相同的结果。

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. 在我的情况下,因为我只使用devDependencies,我很高兴使用相同/最新版本的所有内容,因为我知道它一切都可以正常部署。

As you can probably tell, I'm fairly new to node.js. 你可能会说,我对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). 如果它是相关的,我在Windows 10上并使用Gi​​t Bash(它似乎基于MinGW)。

In my opinion you are doing things the correct way. 在我看来,你正在以正确的方式做事。 I tend to shy away from installing npm modules globally. 我倾向于回避全球安装npm模块。 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. 你可以压缩整个东西,把它放在Dropbox或任何地方,你知道它拥有一切。

I'm surprised your project directories including node_modules are so large to the point you are noticing performance problems. 我很惊讶您的项目目录包括node_modules是如此之大,以至于您注意到性能问题。 If that is actually the case, I would double check there aren't any unused dependencies in your package.json. 如果确实如此,我会仔细检查你的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 . 如果您担心项目中安装了不在package.json(安装ad hoc)中的deps,则可以删除node_modules目录并重新运行npm install You always want package.json to be an accurate list of your project's deps. 您总是希望package.json成为项目代表的准确列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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