如何使用 npm 全局安装模块?
我最近在 OSX 上安装了 Node.js 和 npm 模块,我认为设置有问题:
npm install [MODULE] is not installing the node.js module to the default path
which is /usr/local/lib/node_modules.
I recently installed Node.js and npm module on OSX and have a problem with the settings I think:
npm install [MODULE] is not installing the node.js module to the default path
which is /usr/local/lib/node_modules.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您想全局安装 npm 模块,请确保使用新的
-g
标志,例如:npm install permanent -g
有关 npm 模块安装的一般建议1.0rc(取自 blog.nodejs.org):
我最近刚刚使用了这个建议,并且效果非常顺利。我在全球范围内永久安装(因为它是一个命令行工具),并在本地安装了所有应用程序模块。
但是,如果您想在全局范围内使用某些模块(即express或mongodb),请采纳此建议(也取自blog.nodejs.org):
我没有测试这些变体之一,但它们似乎非常简单。
If you want to install a npm module globally, make sure to use the new
-g
flag, for example:npm install forever -g
The general recommendations concerning npm module installation since 1.0rc (taken from blog.nodejs.org):
I just recently used this recommendations and it went down pretty smoothly. I installed forever globally (since it is a command line tool) and all my application modules locally.
However, if you want to use some modules globally (i.e. express or mongodb), take this advice (also taken from blog.nodejs.org):
I did not test one of those variations, but they seem to be pretty straightforward.
在 Mac 上,我发现输出包含我正在查找的信息:
将
/usr/local/share/npm/bin
添加到我的export PATH
行后code>.bash_profile,保存它,并source
它,我能够正常运行。
On a Mac, I found the output contained the information I was looking for:
After adding
/usr/local/share/npm/bin
to theexport PATH
line in my.bash_profile
, saving it, andsource
ing it, I was able to runnormally.
我喜欢在应用程序文件夹的根目录中使用 package.json 文件。
这是我用的一个
I like using a package.json file in the root of your app folder.
Here is one I use
您需要拥有超级用户权限,
You need to have superuser privileges,
我在 Ubuntu 上安装 Express 时遇到问题:
如果由于某种原因缺少 NPM 命令,请使用
npm help
测试 npm 命令。如果不存在,请按照以下步骤操作 - http:// /arnolog.net/post/8424207595/installing-node-js-npm-express-mongoose-on-ubuntu如果只是 Express 命令不起作用,尝试:
这使得一切都像我习惯的 Windows7 和 OSX 一样工作。
希望这有帮助!
I had issues installing Express on Ubuntu:
If for some reason NPM command is missing, test npm command with
npm help
. If not there, follow these steps - http://arnolog.net/post/8424207595/installing-node-js-npm-express-mongoose-on-ubuntuIf just the Express command is not working, try:
This made everything work as I'm used to with Windows7 and OSX.
Hope this helps!
根据官方文档推荐的步骤在我的 Macbook 上对我有用。
摘要步骤:
使用以下命令实例化 nvm:
curl -o- https://raw.githubusercontent .com/nvm-sh/nvm/v0.38.0/install.sh | bash
您可以在此页面查看最新版本:
https://github.com/nvm-sh/nvm
创建 .zshrc主目录(如果文件不存在)。
触摸.zshrc
将以下内容放入.zshrc文件
Recommended steps as per official doc worked for me on my Macbook.
Summary steps:
Instance nvm using following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
You can check the latest version on this page:
https://github.com/nvm-sh/nvm
create .zshrc at home directory if file is already not present.
touch .zshrc
put following content in .zshrc file
由于 npm 版本 8
-g
或--global
标志已根据警告被弃用,我进入控制台(官方文档尚未更新):所以新命令全局安装模块 其中
forever
是您要安装的软件包的名称。Since npm version 8
-g
or--global
flag is deprecated as per warning I'm getting in the console (official docs yet to be updated):So new command to install modules globally is
Where
forever
is the name of the package you want to install.在Ubuntu中,在.bashrc文件中设置node_modules的路径
In Ubuntu, set path of node_modules in .bashrc file
您可能没有在全局位置(例如
/usr/local/lib/node_modules
)安装节点模块的写入权限,在这种情况下,请以 root 身份运行 npm install -g package。You might not have write permissions to install a node module in the global location such as
/usr/local/lib/node_modules
, in which case run npm install -g package as root.