npm全局路径前缀

我比平时更加​​谨慎,因为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.

西里神奇2020/04/07 11:34:01

简单的解决方案是...

只需将下面的命令:

  1. sudo npm config get prefix

    如果不是这些/usr/local,则需要使用以下命令对其进行修复。

  2. sudo npm config set prefix /usr/local...

现在100%工作正常

蛋蛋2020/04/07 11:34:01

sudo brew不再是一个选项,因此,如果此时使用brew安装,您将获得2个令人讨厌的东西:A:它喜欢安装在/usr/local/opts/ usr / local / shared中或根据此安装最初这没什么大不了的,但是我遇到了节点PATH的问题,尤其是在安装lint的时候。B:您有点喜欢sudo命令,直到您以这种方式卸载并安装它,或者可以从Bitnami获取堆栈

我建议在堆栈选项上使用此方法,因为如果您有多个项目,则可以使用该方法。如果您使用预制的MEAN堆栈,则必须在httpd.conf中设置虚拟主机(此堆栈中比XAMPP还要痛苦的多),并且通常会为每一个额外的位置更新extra / vhosts.conf和/ etc / hosts项目,除非您要完成升级后再重新指向服务器并重新启动服务器。

LGil2020/04/07 11:34:01

如果您已使用sudo命令链接了节点程序包

然后转到全局安装了node_modules的文件夹。

在Unix系统上,全局安装时,它们通常位于/ usr / local / lib / node或/ usr / local / lib / node_modules中。如果将NODE_PATH环境变量设置为此路径,则可以按节点找到模块。

Windows XP-%USERPROFILE%\ Application Data \ npm \ node_modules Windows 7-%AppData%\ npm \ node_modules

然后运行命令

ls -l

这将提供所有全局node_modules的列表,您可以轻松查看链接的节点模块。

西里神奇2020/04/07 11:34:01

尝试运行:

PATH=$PATH:~/npm/bin

然后express在命令行中运行进行测试这对我有用。

飞云小卤蛋2020/04/07 11:34:01

任何人都有与brew和npm之间的冲突有关的相同问题,请检查此解决方案https://gist.github.com/DanHerbert/9520689

西门2020/04/07 11:34:01

我使用brew和前缀已经设置为:

$ npm config get prefix
/Users/[user]/.node

我确实注意到bin和lib文件夹归root所有,这阻止了通常的非sudo安装,因此我将它们重新拥有给用户

$ cd /Users/[user]/.node
$ chown -R [user]:[group] lib
$ chown -R [user]:[group] bin

然后,我将路径添加到位于/ Users / [user]的.bash_profile中

PATH=$PATH:~/.node/bin
小胖Gil2020/04/07 11:34:01

通过执行以下操作,我设法修复了Vue Cli no命令错误:

  • 在终端sudo nano ~/.bash_profile中编辑您的bash配置文件。
  • export PATH=$PATH:/Users/[your username]/.npm-packages/bin
  • 保存文件并重启终端
  • 现在,你应该能够使用vue create my-projectvue --version等。

我从https://cli.vuejs.org/安装了最新的Vue Cli之后执行了此操作

我通常使用yarn,但是我在npm全局安装了它npm install -g @vue/cli您也可以使用纱线yarn global add @vue/cli

注意:如果您已经安装了它,则可能必须先全局将其卸载: npm uninstall -g vue-cli

希望这可以帮助!

达蒙2020/04/07 11:34:01

在这个问题上花了一段时间,并且PATH开关没有帮助。我的问题是在这里找到的Homebrew / node / npm错误-https: //github.com/npm/npm/issues/3794

如果您已经使用Homebrew安装了节点,请尝试****注意每个注释,这可能并不安全。它对我有用,但可能会带来意想不到的后果。看来最新版本的Homebrew可以正确安装npm。所以,很可能我会尝试brew updatebrew doctorbrew upgrade node等试之前****:

npm update -gf

或者,如果要使用Homebrew安装节点并运行npm,请使用:

brew install node --without-npm
curl -L https://npmjs.org/install.sh | sh