我比平时更加谨慎,因为npm
过去的行为使我感到困惑。
我在Mac上,并通过安装的node.js brew install node
。
现在,我想jslint.js
在命令行中作为命令运行,jslint
我发现完成此操作的规范方法是sudo npm install -g jslint
成功运行此输出的:
$ 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) 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
)
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).
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.
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.
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.
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!
Furthermore I recommend ESLint over jslint, and have not used jslint in years.
简单的解决方案是...
只需将下面的命令:
sudo npm config get prefix
如果不是这些
/usr/local
,则需要使用以下命令对其进行修复。sudo npm config set prefix /usr/local
...现在100%工作正常