node.js 全局节点模块安装不正确。找不到相关命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15054388/
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 13:41:19  来源:igfitidea点击:

Global Node modules not installing correctly. Command not found

node.jsterminalinstallnpm

提问by im_benton

I am having a problem installing global node modules and everything I find online says the solve is just adding -g. Which is not the problem. I believe it's a linking issue or wrong directory issue.

我在安装全局节点模块时遇到问题,我在网上找到的所有内容都说解决方案只是添加 -g。这不是问题。我相信这是一个链接问题或错误的目录问题。

Here is what I do:

这是我所做的:

$ npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.3
...downloads correctly

$ express myapp
bash: express: command not found

However when I run the direct link location to express it works:

但是,当我运行直接链接位置来表达它时:

   $ /usr/local/share/npm/bin/express myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
... Builds app correctly

Where the module is:

模块在哪里:

$ which node
/usr/local/bin/node
$ node -pe process.execPath
/usr/local/Cellar/node/0.8.20/bin/node
$ npm link express
/Users/bentonrr/Development/Personal/node_modules/express -> /usr/local/share/npm/lib/node_modules/express

In my .bash_profile I have:

在我的 .bash_profile 我有:

export PATH=/usr/local/bin:$PATH
export NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node

Do I need to change my Node environment to download to correct folder? Is something not linking correctly? I am lost..

我是否需要更改我的 Node 环境才能下载到正确的文件夹?是不是链接不正确?我搞不清楚了..

Thanks!

谢谢!

Other Specs:

其他规格:

$ node --version
v0.8.20
$ npm --version
1.2.11
$ brew --version
0.9.4
OSX Version 10.8.2

采纳答案by Tim Smith

Add /usr/local/share/npm/binto your PATH(e.g., in .bashrc).

添加/usr/local/share/npm/bin到您的PATH(例如,在.bashrc)。

For more info, see npm help npm:

有关更多信息,请参阅npm help npm

global mode: npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.

全局模式:npm 将包安装到 prefix/lib/node_modules 的安装前缀中,bins 安装在 prefix/bin 中。

You can find the install prefix with npm get prefixor npm config list | grep prefix.

您可以使用npm get prefix或找到安装前缀npm config list | grep prefix

回答by Brad Parks

This may mean your node install prefix isn't what you expect.

这可能意味着您的节点安装前缀不是您所期望的。

You can set it like so:

你可以这样设置:

npm config set prefix /usr/local

npm config set prefix /usr/local

then try running npm install -gagain, and it should work out. Worked for me on a mac, and the solution comes from this site:

然后npm install -g再次尝试运行,它应该会成功。在 Mac 上对我来说有效,解决方案来自此站点:

http://webbb.be/blog/command-not-found-node-npm/

http://webbb.be/blog/command-not-found-node-npm/

EDIT: Note that I just came across this again on a new Mac I'm setting up, and had to do the process detailed here on stackoverflowas well.

编辑:请注意,我刚刚在我正在设置的新 Mac 上再次遇到了这个问题,并且还必须在 stackoverflow 上执行此处详述的过程。

回答by Vicente

My npm couldn't find global packages as well. I did what Brad Parks suggested:

我的 npm 也找不到全局包。我做了布拉德帕克斯的建议:

npm config set prefix /usr/local

Then I got a EACCES permissions error (DON'T USE sudo npm install -g <package>) and fixed it through the official npm docs: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

然后我得到一个 EACCES 权限错误(不要使用sudo npm install -g <package>)并通过官方 npm 文档修复它:https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

  1. On the command line, in your home directory, create a directory for global installations:
  1. 在命令行上,在您的主目录中,为全局安装创建一个目录:
 mkdir ~/.npm-global
  1. Configure npm to use the new directory path:
  1. 配置 npm 以使用新的目录路径:
 npm config set prefix '~/.npm-global'
  1. In your preferred text editor, open or create a ~/.profile file and add this line:
  1. 在您首选的文本编辑器中,打开或创建一个 ~/.profile 文件并添加以下行:
 export PATH=~/.npm-global/bin:$PATH
  1. On the command line, update your system variables:
  1. 在命令行上,更新您的系统变量:
 source ~/.profile
  1. Then install a package globally and test it! For example:
  1. 然后全局安装一个包并测试它!例如:
npm install -g awsmobile-cli
awsmobile configure

回答by lazyTank

In my case, The NODE_PATH environment variable was empty. Check whether it is empty-

就我而言, NODE_PATH 环境变量为空。检查它是否为空-

echo $NODE_PATH

if the NODE_PATH is empty. Then change ~/.bash_profile and add NODE_PATH

如果 NODE_PATH 为空。然后更改 ~/.bash_profile 并添加 NODE_PATH

nano ~/.bash_profile
export NODE_PATH=`npm root -g`
source ~/.bash_profile

Now install npm modules again and check whether that is being installed on the path npm root -g

现在再次安装 npm 模块并检查它是否安装在路径上 npm root -g

回答by blamb

I do not everinstall any npm stuff, via sudo! I have my own reasons, but I just try to keep things simple, and user based, since this is a user development world, and not everyone has root access, and root/sudo installing things like this just seems to clutter up things to begin with. After all, all developers should be able to follow these instructions, not just privileged sudo users.

从不通过 sudo 安装任何 npm 东西!我有我自己的理由,但我只是尽量让事情简单化,并以用户为基础,因为这是一个用户开发世界,并不是每个人都拥有 root 访问权限,并且 root/sudo 安装这样的东西似乎只是让事情开始变得混乱和。毕竟,所有开发人员都应该能够遵循这些说明,而不仅仅是特权 sudo 用户。

This particular system is a RHEL7 accessed via SSH:

这个特定的系统是一个通过 SSH 访问的 RHEL7:

Frequently one needs various versions of node, so I use NVM https://github.com/creationix/nvm

经常需要各种版本的节点,所以我使用 NVM https://github.com/creationix/nvm

So with that said, I can show you a working example for -gglobal installs, using NVM, NPM, and node paths not using root.

话虽如此,我可以向您展示一个-g全局安装的工作示例,使用 NVM、NPM 和不使用 root 的节点路径。

set your prefix for .npm-packagesif it isn't already. (note, thats a hyphen, not an underscore)

.npm-packages如果还没有,请设置您的前缀。(注意,这是一个连字符,而不是下划线)

nvm config ls
prefix = "/home/<yourusername>/.npm-packages"

Then adjust your ~/.bash_profile or .bashrc if you prefer readup on why and which here, with the following information.

然后调整你的 ~/.bash_profile 或 .bashrc 如果你更喜欢阅读 为什么和这里的内容,并提供以下信息。

#PATH EXPORTS
NODE_MODULES=$HOME/.npm                                          
NPM_PACKAGES=$HOME/.npm-packages/bin                           
export PATH=$PATH:$HOME/bin:$NODE_MODULES:$NPM_PACKAGES         

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

That pretty much covers all paths. For e.g., if you install gulp like this npm install -g gulpit symlinks in ~/.npm-packages/bin(note thats a hyphen, not an underscore). (no need for gulp-cli, or gulp-cl)

这几乎涵盖了所有路径。例如,如果您像这样安装 gulp,npm install -g gulp它会在其中进行符号链接~/.npm-packages/bin(请注意,这是一个连字符,而不是下划线)。(不需要gulp-cli, 或gulp-cl

You can pretty much replace/comment-out all other node path exports. You can put this path info below any other path info you already have, safely, without it overwriting that stuff.

您几乎可以替换/注释掉所有其他节点路径导出。您可以将此路径信息安全地放在您已有的任何其他路径信息之下,而不会覆盖这些内容。

回答by Lasithds

  1. Add the following line to your ~/.bash_profile

    export PATH="$HOME/.npm/bin:$PATH"
    
  2. Load bash profile

    bash -l
    
  1. 将以下行添加到您的 ~/.bash_profile

    export PATH="$HOME/.npm/bin:$PATH"
    
  2. 加载 bash 配置文件

    bash -l
    

回答by Adedoyin Akande

For Windows users

对于 Windows 用户

Add this to your path:"%AppData%\npm"

将此添加到您的路径:“%AppData%\npm”

回答by Rossa D

Check your global Node module's binary folder, and add it to your $PATH.

检查全局节点模块的二进制文件夹,并将其添加到 $PATH。

npm list -g | head -1

If you use nodenv, the path will change whenever you install the global node version. Adding the path like this solves my problem.

如果使用nodeenv,则每次安装全局节点版本时,路径都会更改。添加这样的路径解决了我的问题。

"$HOME/.nodenv/versions/$(nodenv global)/bin"

Shortcut for adding the path to zsh

添加zsh路径的快捷方式

$ echo 'export PATH="$HOME/.nodenv/versions/$(nodenv global)/bin"' >> ~/.zshrc

回答by dimpen

The problem I had was missing the binariesbecause the user specific .npmrcfile in my home directory had bin-linksset to false, though the default is true.

我的问题是缺少二进制文件,因为用户特定.npmrc在我的主目录文件已经bin-links设置false,虽然default is true

Just in case this is your problem check that none of your .npmrc files have it set to false.

以防万一这是您的问题,请检查您的 .npmrc 文件中是否没有将其设置为 false。

Then re-installing all modules will create the binaries at the prefixso your PATHcan see them.

然后重新安装所有模块将创建二进制文件,prefix以便您PATH可以看到它们。

回答by Luke B_

Steps

脚步

First, remove Node:

首先,删除节点:

 sudo rm -rf /usr/local/lib/node_modules/jitsu
 npm cache clear
 sudo npm install jitsu -g

Second, create .bash_rc:

其次,创建 .bash_rc:

 vi ~/.bash_rc

Copy following items and paste into the file, opened in step 2:

复制以下项目并粘贴到在步骤 2 中打开的文件中:

 [[ -s  ~/.bashrc ]] && source ~/.bashrc
 export PATH=/usr/local/share/npm/bin:$PATH

Run Jitsu. Run vi ~/.bash_profile, this is what you should see:

跑柔术。运行 vi ~/.bash_profile,这是你应该看到的:

 [[ -s  ~/.bashrc ]] && source ~/.bashrc
 export PATH=/usr/local/share/npm/bin:$PATH

 # {{{
 # Node Completion - Auto-generated, do not touch.
 shopt -s progcomp
 for f in $(command ls ~/.node-completion); do
   f="$HOME/.node-completion/$f"
   test -f "$f" && . "$f"
 done
 # }}}

I found the answer for removing Node from this article: JITSU FAILED TO INSTALL OSX [node 0.8.17 and NPM 1.2.0] WTF

我从这篇文章中找到了删除 Node 的答案: JITSU FAILED TO INSTALL OSX [node 0.8.17 and NPM 1.2.0] WTF