使用 package.json 全局和本地安装依赖项 [英] Install dependencies globally and locally using package.json

查看:92
本文介绍了使用 package.json 全局和本地安装依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 npm,我们可以使用 -g 选项全局安装模块.我们如何在 package.json 文件中执行此操作?

Using npm we can install the modules globally using -g option. How can we do this in the package.json file?

假设,这些是我在 package.json 文件中的依赖项

Suppose, these are my dependencies in package.json file

"dependencies": {
    "mongoose": "1.4.0",
    "node.io" : "0.3.3",
    "jquery"  : "1.5.1",
    "jsdom"   : "0.2.0",
    "cron"    : "0.1.2"
  }

当我运行 npm install 时,我只想全局安装 node.io,其余的应该在本地安装.有这个选项吗?

When i run npm install, I want only node.io to be installed globally, the rest others should be installed locally. Is there an option for this?

推荐答案

新注意事项:您可能不想或不需要这样做.您可能想要做的只是将构建/测试等的这些类型的命令依赖项放在 package.json 的 devDependencies 部分.任何时候你在 package.json 中使用 scripts 中的东西,你的 devDependencies 命令(在 n​​ode_modules/.bin 中)就像它们在你的路径中一样.

New Note: You probably don't want or need to do this. What you probably want to do is just put those types of command dependencies for build/test etc. in the devDependencies section of your package.json. Anytime you use something from scripts in package.json your devDependencies commands (in node_modules/.bin) act as if they are in your path.

例如:

npm i --save-dev mocha # Install test runner locally
npm i --save-dev babel # Install current babel locally

然后在 package.json 中:

Then in package.json:

// devDependencies has mocha and babel now

"scripts": {
  "test": "mocha",
  "build": "babel -d lib src",
  "prepublish": "babel -d lib src"
}

然后在你的命令提示符下你可以运行:

Then at your command prompt you can run:

npm run build # finds babel
npm test # finds mocha

npm publish # will run babel first

新注意:一段时间以来,我们已经有了 npx,它允许您运行 devDependencies 命令,而无需将它们添加到您的 脚本代码> 部分(如果需要).例如:

New NEW Note: For awhile now we have had npx, which allows you to run the devDependencies commands without needing to add them to your scripts section (if you want). For example:

npx webpack

但是如果你真的想全局安装,你可以在 package.json 的脚本部分添加一个预安装:

But if you really want to install globally, you can add a preinstall in the scripts section of the package.json:

"scripts": {
  "preinstall": "npm i -g themodule"
}

所以实际上我的 npm install 再次执行 npm install .. 这很奇怪但似乎有效.

So actually my npm install executes npm install again .. which is weird but seems to work.

注意:如果您使用 npm 的最常见设置,其中全局 Node 包安装所需的 sudo,您可能会遇到问题.一种选择是更改您的 npm 配置,因此这不是必需的:

Note: you might have issues if you are using the most common setup for npm where global Node package installs required sudo. One option is to change your npm configuration so this isn't necessary:

npm config set prefix ~/npm,通过附加 export PATH=$HOME/npm/bin:$PATH 到 $PATH 添加 $HOME/npm/bin你的 ~/.bashrc.

npm config set prefix ~/npm, add $HOME/npm/bin to $PATH by appending export PATH=$HOME/npm/bin:$PATH to your ~/.bashrc.

另一个可能更好的选择是只使用 nvm 来管理 Node,你就不会有这个问题了.

Another, probably better option is to just use nvm to manage Node and you won't have that problem.

这篇关于使用 package.json 全局和本地安装依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆