node.js npm 全局路径前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14803978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:08:16  来源:igfitidea点击:

npm global path prefix

macosnode.jsnpmzsh

提问by Steven Lu

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

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

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

我在Mac上,并通过安装的node.js brew install node

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

现在我想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
[email protected] /usr/local/share/npm/lib/node_modules/jslint
└── [email protected] ([email protected])

Subsequently

随后

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

due to /usr/local/share/npm/binnot being in my $PATH.

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

1) Why did brewnot install global npmbinpath to path? Maybe it did, but something zshdoes is messing it up. Where might I be able to find that?
2) Should I do this? (Append :/usr/local/share/npm/binto the exported $PATHat the bottom of my ~/.zshrc)

1)为什么没有brew安装全局npmbin路径到路径?也许确实如此,但zsh确实有些事情搞砸了。我在哪里可以找到它?
2)我应该这样做吗?(附加:/usr/local/share/npm/bin$PATH我底部的导出~/.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 -gnot making the right symlinks in a "proper" location (like /usr/local/binmaybe).

这似乎不是正确的方法,因为如果我稍后安装其他东西(使用 Homebrew 或其他东西),我将需要将它附加到我的 zsh 启动脚本以设置路径。我想在这个特定的例子中,这只是npm install -g没有在“适当”的位置(比如/usr/local/bin也许)制作正确的符号链接的问题。

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

我想我要做的是/usr/local/bin为我遇到问题的任何程序手动构建符号链接,它应该足以满足我的目的。

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.

编辑:我将在 2019 年更新这个,因为这个老问题很受欢迎,但现在就我的个人工作流程以及如何最好地安装 node 和管理其各种依赖项而言都过时了。

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.

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

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!

另外,需要指出的最重要的一点是,非常不鼓励使用 sudo,如我上面的 jslint 示例的 npm install 所示;我相当肯定,永远不应该使用超级用户以任何方式安装自制软件包或与之交互。它应该不是必需的,并且会导致文件权限问题!

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

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

回答by Trevor Burnham

Extending your PATHwith:

扩展你PATH的:

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

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

不是一个可怕的主意。话虽如此,你不应该这样做。

Run this:

运行这个:

npm config get prefix

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

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

So:

所以:

  1. npm config set prefix /usr/localif it's something else, and
  2. Don't use sudowith npm! According to the jslint docs, you should just be able to npm installit.
  1. npm config set prefix /usr/local如果是别的东西,并且
  2. 不要sudo与 npm 一起使用!根据jslint docs,您应该能够npm install做到。

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 安装为 sudo ( sudo brew install),请尝试使用普通 ol' 重新安装它brew install。Homebrew 应该可以帮助你保持sudo自由。

回答by YPCrumble

Spent a while on this issue, and the PATH switch wasn't helping. My problem was the Homebrew/node/npm bug found here - https://github.com/npm/npm/issues/3794

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

If you've already installed node using Homebrew, try ****Note per comments that this might not be safe. It worked for me but could have unintended consequences. It also appears that latest version of Homebrew properly installs npm. So likely I would try brew update, brew doctor, brew upgrade nodeetc before trying****:

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

npm update -gf

Or, if you want to install node with Homebrew and have npm work, use:

或者,如果您想使用 Homebrew 安装节点并让 npm 工作,请使用:

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

回答by Greg

I use brew and the prefix was already set to be:

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

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

I did notice that the bin and lib folder were owned by root, which prevented the usual non sudo install, so I re-owned them to the user

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

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

Then I just added the path to my .bash_profile which is located at /Users/[user]

然后我只是添加了我的 .bash_profile 的路径,它位于 /Users/[user]

PATH=$PATH:~/.node/bin

回答by Pascal Belloncle

brew should not require you to use sudo even when running npm with -g. This might actually create more problems down the road.

即使在使用 -g 运行 npm 时,brew 也不应该要求您使用 sudo。这实际上可能会在未来产生更多问题。

Typically, brew or port let you update you path so it doesn't risk messing up your .zshrc, .bashrc, .cshrc, or whatever flavor of shell you use.

通常, brew 或 port 允许您更新路径,因此不会有弄乱您的 .zshrc、.bashrc、.cshrc 或您使用的任何类型的 shell 的风险。

回答by Mark Fasel

I managed to fix Vue Cli no command error by doing the following:

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

  • In terminal sudo nano ~/.bash_profileto edit your bash profile.
  • Add export PATH=$PATH:/Users/[your username]/.npm-packages/bin
  • Save file and restart terminal
  • Now you should be able to use vue create my-projectand vue --versionetc.
  • 在终端sudo nano ~/.bash_profile中编辑您的 bash 配置文件。
  • 添加 export PATH=$PATH:/Users/[your username]/.npm-packages/bin
  • 保存文件并重启终端
  • 现在,你应该能够使用vue create my-projectvue --version等。

I did this after I installed the latest Vue Cli from https://cli.vuejs.org/

我在从https://cli.vuejs.org/安装了最新的 Vue Cli 之后这样做了

I generally use yarn, but I installed this globally with npm npm install -g @vue/cli. You can use yarn too if you'd like yarn global add @vue/cli

我通常使用 yarn,但我使用 npm 全局安装了它npm install -g @vue/cli。如果你愿意,你也可以使用纱线yarn global add @vue/cli

Note: you may have to uninstall it first globally if you already have it installed: npm uninstall -g vue-cli

注意:如果你已经安装了它,你可能需要先全局卸载它: npm uninstall -g vue-cli

Hope this helps!

希望这可以帮助!

回答by Bdwey

Any one got the same issue it's related to a conflict between brew and npm Please check this solution https://gist.github.com/DanHerbert/9520689

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

回答by Yogesh Kumar

Simple solution is ...

简单的解决方法是...

Just put below command :

只需将下面的命令:

  1. sudo npm config get prefix

    if it's not something like these /usr/local, than you need to fix it using below command.

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

  1. sudo npm config get prefix

    如果它不是这样的/usr/local,那么你需要使用下面的命令来修复它。

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

Now it's 100% working fine

现在它 100% 工作正常

回答by kiko carisse

Try running:

尝试运行:

PATH=$PATH:~/npm/bin

and then do a test by running expressin the command line. This worked for me.

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

回答by selftaught91

If you have linked the node packages using sudo command

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

Then go to the folder where node_modules are installed globally.

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

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

在 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

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

and then run the command

然后运行命令

ls -l

This will give the list of all global node_modules and you can easily see the linked node modules.

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

回答by angel

sudo brew is no longer an option so if you install with brew at this point you're going to get 2 really obnoxious things: A: it likes to install into /usr/local/optsor according to this, /usr/local/shared. This isn't a big deal at first but i've had issues with node PATH especially when I installed lint. B: you're kind of stuck with sudo commands until you either uninstall and install it this way or you can get the stack from Bitnami

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

I recommend this method over the stack option because it's ready to go if you have multiple projects. If you go with the premade MEAN stack you'll have to set up virtual hosts in httpd.conf (more of a pain in this stack than XAMPP)plust the usual update your extra/vhosts.conf and /etc/hosts for every additional project, unless you want to repoint and restart your server when you get done updatading things.

我推荐此方法而不是堆栈选项,因为如果您有多个项目,它已准备就绪。如果你使用预制的 MEAN 堆栈,你将不得不在 httpd.conf 中设置虚拟主机(这个堆栈比 XAMPP 更痛苦)加上通常更新你的 extra/vhosts.conf 和 /etc/hosts 为每个额外项目,除非您想在完成更新后重新指向并重新启动服务器。