node.js 使用 npm 安装后在 bash 中找不到 express 命令

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

express command not found in bash after installing it with npm

bashnode.jsexpressnpm

提问by Sander

just installed new ubuntu vm to test around with node installed things in this order:

刚刚安装了新的 ubuntu vm 以按以下顺序使用节点安装的东西进行测试:

node
mongodb-server
npm
express
mongoose

now, trying to create a new app i noticed express cannot be used in the shell. express -vreturns express: command not found

现在,尝试创建一个新的应用程序,我注意到 express 无法在 shell 中使用。 express -v返回快递:找不到命令

i installed npm like this

我像这样安装了 npm

curl http://npmjs.org/install.sh | sudo sh

and i installed express this way

我以这种方式安装了快递

npm install express

any ideas?

有任何想法吗?

回答by Fazi

Starting from express 4.00 you also need to install express generator with:

从 express 4.00 开始,您还需要安装 express 生成器:

npm install -g express-generator

Only after this will you be able to run express as a command!

只有在此之后,您才能将 express 作为命令运行!

For confirmation see: ExpressJS.com - Migrating to Express 4

如需确认,请参阅:ExpressJS.com - 迁移到 Express 4

回答by Raynos

npm install express -g

npm install express -g

You need to install it globally.

您需要全局安装它。

Npm 1.0 installs modules locally by default. So the bash executable lives in /node_modules/bin/. You can add that folder to PATHor you can just install expressglobally so that it's picked up by PATH

Npm 1.0 默认在本地安装模块。所以 bash 可执行文件存在于/node_modules/bin/. 您可以将该文件夹添加到,PATH或者您可以express全局安装,以便它被PATH

回答by Warren Reilly

I had this problem and was installing node via Homebrew. The problem was being caused by Homebrew.

我遇到了这个问题,正在通过 Homebrew 安装节点。问题是由 Homebrew 引起的。

So I did:

所以我做了:

brew uninstall node

and then installed node using the installer on the nodejs.orgsite.

然后使用nodejs.org站点上的安装程序安装节点。

Then I ran:

然后我跑了:

npm install -g express

And voila no problems.

瞧,没有问题。

回答by hippopots

EDIT2017-06-29: this answer is 6+ years old, but still gets votes/traffic. Instead (for any new users with problems)I'd trust both NODE_PATHofficial docand its corresponding bit about REPL usagebefore this answer.

编辑2017-06-29:这个答案已有 6 年以上的历史,但仍然获得选票/流量。相反(对于任何有问题的新用户),在此答案之前,我会信任NODE_PATH官方文档及其有关 REPL 使用的相应部分

Quite similar to this issue, node was not finding my global express install, so a require('express')statement would fail.

与此问题非常相似,节点没有找到我的全局快速安装,因此require('express')语句将失败。

What fixed this for me, when a global install wasn't being picked up by node was making sure NODE_PATHenv. variablewas is set correctly. On Ubuntu 11.04, with node version 0.5.0-pre, the paths me were:

当节点没有选择全局安装时,为我解决了这个问题的是确保NODE_PATHenv. 变量was 设置正确。在 Ubuntu 11.04 上,节点版本为 0.5.0-pre,我的路径是:

NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node

So, to clarify you might want to export the above env. variable, or you can just test the above values out by doing:

因此,为了澄清您可能想要导出上述 env。变量,或者您可以通过执行以下操作来测试上述值:

NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node node ./you_app.js

回答by Asaf Manassen

With the release of Express 4.0.0 it looks like you need to do sudo npm install -g express-generator.

随着 Express 4.0.0 的发布,您似乎需要做 sudo npm install -g express-generator.

回答by maudulus

I had to do a combination of things:

我不得不做一些事情:

  1. From node.js modules path:

    echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bash_profile && . ~/.bash_profile
    

    This sets the file path in bash profile (can be viewed using nano .bash_profile

  2. Slightly modified from Raynos (above) since I needed sudo:

    sudo npm install express -g
    
  3. Slightly modified from Fazi (above) since I needed sudo:

    sudo npm install -g express-generator
    
  1. node.js 模块路径

    echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bash_profile && . ~/.bash_profile
    

    这将设置 bash 配置文件中的文件路径(可以使用 nano .bash_profile

  2. 由于我需要,从 Raynos(上图)稍作修改sudo

    sudo npm install express -g
    
  3. 由于我需要,对 Fazi(上图)稍作修改sudo

    sudo npm install -g express-generator
    

TEST YOUR APPLICATION:

测试您的应用程序:

run `DEBUG=myapp:* npm start`

Ref: http://expressjs.com/en/starter/generator.html

参考:http: //expressjs.com/en/starter/generator.html

回答by Mark Karwowski

IF you are running windows:

如果您正在运行 Windows:

export NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"