npm全局路径前缀 [英] npm global path prefix

查看:150
本文介绍了npm全局路径前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比平时更加​​谨慎,因为过去我对npm的行为感到困惑.

I am being more cautious than usual because I have been confused by the behavior of npm in the past.

我在Mac上,并且已经通过brew install node安装了node.js.

I am on a Mac and have installed node.js through brew install node.

现在,我想在命令行上以命令jslint的形式运行jslint.js,我发现完成此操作的规范方法是sudo npm install -g jslint,该命令成功运行了以下输出:

Now that I want to run jslint.js on the command-line as the command jslint I find that the canonical way to accomplish this is sudo npm install -g jslint which ran successfully with this output:

$ sudo npm install -g jslint
npm http GET https://registry.npmjs.org/jslint
npm http 200 https://registry.npmjs.org/jslint
npm http GET https://registry.npmjs.org/jslint/-/jslint-0.1.9.tgz
npm http 200 https://registry.npmjs.org/jslint/-/jslint-0.1.9.tgz
npm http GET https://registry.npmjs.org/nopt
npm http 200 https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz
npm http 200 https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz
npm http GET https://registry.npmjs.org/abbrev
npm http 200 https://registry.npmjs.org/abbrev
npm http GET https://registry.npmjs.org/abbrev/-/abbrev-l.0.4.tgz
npm http 200 https://registry.npmjs.org/abbrev/-/abbrev-1.0.4.tgz
/usr/local/share/npm/bin/jslint -> /usr/local/share/npm/lib/node_modules/jslint/
bin/jslint.js
jslint@0.1.9 /usr/local/share/npm/lib/node_modules/jslint
└── nopt@1.0.10 (abbrev@1.0.4)

随后

$ jslint ply.js
zsh: command not found: jslint

由于/usr/local/share/npm/bin不在我的$PATH中.

1)为什么brew没有安装全局npm bin路径到路径?也许是这样,但是zsh所做的事情却把它弄乱了.我在哪里可以找到呢?
2)我应该这样做吗? (将:/usr/local/share/npm/bin附加到我的~/.zshrc底部的导出的$PATH上)

1) Why did brew not install global npm bin path to path? Maybe it did, but something zsh does is messing it up. Where might I be able to find that?
2) Should I do this? (Append :/usr/local/share/npm/bin to the exported $PATH at the bottom of my ~/.zshrc)

这似乎不是正确的方法,因为如果以后再安装其他东西(使用Homebrew或其他方式),则需要将其附加到我的zsh启动脚本中以设置路径.我想在这种特殊情况下,这只是npm install -g不能在适当"位置(可能是/usr/local/bin)进行正确符号链接的问题.

It seems like this isn't the right way to do it because if I install something else later (using Homebrew or something) I'll be needing to append it to my zsh startup script to set the path. I guess in this particular instance it's just a matter of the npm install -g not making the right symlinks in a "proper" location (like /usr/local/bin maybe).

我认为我会做的是在/usr/local/bin中为遇到问题的任何程序手动建立符号链接,对于我的目的来说,它应该足够好.

I think what I will do is manually build out symlinks inside /usr/local/bin for any programs that I have trouble with and it should be good enough for my purposes.

编辑:我将在2019年对此进行更新,因为这个老问题很受欢迎,但现在在我的个人工作流程以及如何最好地安装节点和管理方面都已经过时了它的各种依赖关系.

EDIT: I'm updating this in 2019, because this old question is popular but now outdated both in terms of my personal workflow and also in terms of how to best go about installing node and managing its various dependencies.

我现在的个人工作流程是使用从nodejs.org获得的安装程序在系统上安装node.实际上,我比自制软件包本身更信任此文件,因为它最了解如何管理文件.如果要将系统节点从8.10更改为10.15,无论是在Mac还是Windows上,只需从nodejs.org下载适用于OS的10.15的安装程序即可.根据我的经验,尝试升级通过自制程序安装的节点一直是比较困难的方法.

My personal workflow is now to use the installer obtained from nodejs.org to install node on systems. I actually trust this more than the homebrew package itself, because it knows best how to manage the files. If you want to change your system node from 8.10 to 10.15, whether on Mac or Windows, simply download the installer from nodejs.org for 10.15 for your OS. Attempting to upgrade node installed via homebrew has always proven to be a more difficult approach in my experience.

最大的一点是,强烈建议不要使用sudo,如我上面的jslint示例的npm安装所示.我相当确定,决不能使用超级用户以任何方式来安装或自制软件包.不必要,这会导致文件许可问题!

Also, the biggest thing to point out is that the use of sudo as shown in my npm install of jslint example above is highly discouraged; I am fairly certain that homebrew packages should never be installed or interacted with in any way using the superuser. It should not be necessary, and will lead to file permission headaches!

此外,我推荐使用ESLint而不是jslint,并且多年来没有使用过jslint.

Furthermore I recommend ESLint over jslint, and have not used jslint in years.

推荐答案

export PATH=/usr/local/share/npm/bin:$PATH

这不是一个糟糕的主意.话虽如此,您不必这样做.

isn't a terrible idea. Having said that, you shouldn't have to do it.

运行此:

npm config get prefix

OS X的默认值为/usr/local,这意味着npm会将二进制文件符号链接到/usr/local/bin,该二进制文件应该已经在您的PATH上(特别是如果您使用的是Homebrew).

The default on OS X is /usr/local, which means that npm will symlink binaries into /usr/local/bin, which should already be on your PATH (especially if you're using Homebrew).

所以:

  1. npm config set prefix /usr/local是否还有其他内容,并且
  2. 不要将sudo与npm一起使用!根据 jslint文档,您应该能够npm install它.
  1. npm config set prefix /usr/local if it's something else, and
  2. Don't use sudo with npm! According to the jslint docs, you should just be able to npm install it.

如果将npm作为sudo(sudo brew install)安装,请尝试使用普通的brew install重新安装.自制软件应该可以帮助您保持sudo自由.

If you installed npm as sudo (sudo brew install), try reinstalling it with plain ol' brew install. Homebrew is supposed to help keep you sudo-free.

这篇关于npm全局路径前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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