如何使用 npm 全局安装模块?

发布于 2024-11-04 07:45:02 字数 190 浏览 3 评论 0原文

我最近在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

最终幸福 2024-11-11 07:45:02

如果您想全局安装 npm 模块,请确保使用新的 -g 标志,例如:

npm install permanent -g

有关 npm 模块安装的一般建议1.0rc(取自 blog.nodejs.org):

  • 如果您要安装要在程序中使用的内容,请使用
    require('whatever'),然后安装它
    本地
    ,在你的根
    项目。
  • 如果您要安装要在 shell 中使用的内容,请在
    命令行什么的,安装
    它在全球范围内,因此它的二进制文件
    最终进入您的 PATH 环境
    变量。


我最近刚刚使用了这个建议,并且效果非常顺利。我在全球范围内永久安装(因为它是一个命令行工具),并在本地安装了所有应用程序模块。

但是,如果您想在全局范围内使用某些模块(即express或mongodb),请采纳此建议(也取自blog.nodejs.org):

当然,也有一些情况
你想两者都做。咖啡脚本和
Express 都是应用程序的好例子
具有命令行界面,如
还有图书馆。在这些情况下,你
可以执行以下操作之一:

  • 将其安装在两个地方。说真的,你的磁盘空间这么小吗?
    空间?真的没关系。它们很小
    JavaScript 程序。
  • 全局安装,然后 npm link Coffee-script 或 npm linkexpress
    (如果您使用的平台支持
    符号链接。)那么你只需要
    更新全局副本以更新所有
    符号链接也是如此。

第一个选项是我心中最好的
观点。简单、清晰、明确。这
如果你是的话,第二个真的很方便
将在一个中重复使用相同的库
一堆不同的项目。 (更多关于
npm 链接将在未来的部分中提供。)

我没有测试这些变体之一,但它们似乎非常简单。

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):

  • If you’re installing something that you want to use in your program, using
    require('whatever'), then install it
    locally
    , at the root of your
    project.
  • If you’re installing something that you want to use in your shell, on the
    command line or something, install
    it globally
    , so that its binaries
    end up in your PATH environment
    variable.

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):

Of course, there are some cases where
you want to do both. Coffee-script and
Express both are good examples of apps
that have a command line interface, as
well as a library. In those cases, you
can do one of the following:

  • Install it in both places. Seriously, are you that short on disk
    space? It’s fine, really. They’re tiny
    JavaScript programs.
  • Install it globally, and then npm link coffee-script or npm link express
    (if you’re on a platform that supports
    symbolic links.) Then you only need to
    update the global copy to update all
    the symlinks as well.

The first option is the best in my
opinion. Simple, clear, explicit. The
second is really handy if you are
going to re-use the same library in a
bunch of different projects. (More on
npm link in a future installment.)

I did not test one of those variations, but they seem to be pretty straightforward.

还如梦归 2024-11-11 07:45:02

在 Mac 上,我发现输出包含我正在查找的信息:

gt; npm install -g karma
...
...
> [email protected] install /usr/local/share/npm/lib/node_modules/karma/node_modules/socket.io/node_modules/socket.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
...
gt; ls /usr/local/share/npm/bin
karma nf

/usr/local/share/npm/bin 添加到我的 export PATH 行后code>.bash_profile,保存它,并source它,我能够

gt; karma --help

正常运行。

On a Mac, I found the output contained the information I was looking for:

gt; npm install -g karma
...
...
> [email protected] install /usr/local/share/npm/lib/node_modules/karma/node_modules/socket.io/node_modules/socket.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
...
gt; ls /usr/local/share/npm/bin
karma nf

After adding /usr/local/share/npm/bin to the export PATH line in my .bash_profile, saving it, and sourceing it, I was able to run

gt; karma --help

normally.

第七度阳光i 2024-11-11 07:45:02

我喜欢在应用程序文件夹的根目录中使用 package.json 文件。

这是我用的一个

nvm use v0.6.4
npm install

I like using a package.json file in the root of your app folder.

Here is one I use

nvm use v0.6.4
npm install
倾城泪 2024-11-11 07:45:02

您需要拥有超级用户权限,

 sudo npm install -g <package name>

You need to have superuser privileges,

 sudo npm install -g <package name>
朕就是辣么酷 2024-11-11 07:45:02

我在 Ubuntu 上安装 Express 时遇到问题:

如果由于某种原因缺少 NPM 命令,请使用 npm help 测试 npm 命令。如果不存在,请按照以下步骤操作 - http:// /arnolog.net/post/8424207595/installing-node-js-npm-express-mongoose-on-ubuntu

如果只是 Express 命令不起作用,尝试:

sudo npm install -g 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-ubuntu

If just the Express command is not working, try:

sudo npm install -g express

This made everything work as I'm used to with Windows7 and OSX.

Hope this helps!

留蓝 2024-11-11 07:45:02

根据官方文档推荐的步骤在我的 Macbook 上对我有用。

摘要步骤:

  1. 使用以下命令实例化 nvm:

    curl -o- https://raw.githubusercontent .com/nvm-sh/nvm/v0.38.0/install.sh | bash

您可以在此页面查看最新版本:
https://github.com/nvm-sh/nvm

  1. 创建 .zshrc主目录(如果文件不存在)。

    触摸.zshrc

  2. 将以下内容放入.zshrc文件

export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && 。
"$NVM_DIR/nvm.sh" # 这会加载 nvm

  1. 使用命令安装 nvm

nvm 安装 --lts

  1. restart 终端 - 您现在已准备好使用 npm 安装全局包。

Recommended steps as per official doc worked for me on my Macbook.

Summary steps:

  1. 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

  1. create .zshrc at home directory if file is already not present.

    touch .zshrc

  2. put following content in .zshrc file

export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && .
"$NVM_DIR/nvm.sh" # This loads nvm

  1. Install nvm using command

nvm install --lts

  1. restart terminal - you are ready to install global package using npm now.
少跟Wǒ拽 2024-11-11 07:45:02

由于 npm 版本 8 -g--global 标志已根据警告被弃用,我进入控制台(官方文档尚未更新):

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

所以新命令全局安装模块 其中

npm install forever --location=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):

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

So new command to install modules globally is

npm install forever --location=global

Where forever is the name of the package you want to install.

舞袖。长 2024-11-11 07:45:02

在Ubuntu中,在.bashrc文件中设置node_modules的路径

导出 PATH="/home/username/node_modules/.bin:$PATH"

In Ubuntu, set path of node_modules in .bashrc file

export PATH="/home/username/node_modules/.bin:$PATH"

面犯桃花 2024-11-11 07:45:02

您可能没有在全局位置(例如 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文