8

惨案

我正常使用命令安装express。

$ npm install -g express-generator

然后bash就提示安装成功

/Users/majialun/.npm-global/bin/express -> /Users/majialun/.npm-global/lib/node_modules/express-generator/bin/express-cli.js
+ express-generator@4.16.1
updated 1 package in 2.123s

然后运行 express,按道理这个时候应该让我创建项目了,但是系统提示:

bash: express: command not found

其实不光是express,我自己写的脚本,也出现这个问题,全局安装成功,但是,通过bash就是调用不出来。

侦查

先看看全局有哪些path变量

$ echo $PATH

然后就出现了很多

/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/Users/majialun/.rvm/bin:/Users/majialun/Documents/flutter/flutter/bin:/Users/majialun/.npm-global/bin/express:/Users/majialun/.rvm/bin:/Users/majialun/Documents/flutter/flutter/bin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/Users/majialun/.rvm/bin:/Users/majialun/Documents/flutter/flutter/bin:/Users/majialun/.npm-global/bin/express:/Users/majialun/.rvm/bin:/Users/majialun/.npm-global/bin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/Users/majialun/.rvm/bin:/Users/majialun/Documents/flutter/flutter/bin:/Users/majialun/.npm-global/bin/express:/Users/majialun/.rvm/bin:/Users/majialun/Documents/flutter/flutter/bin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/majialun/.rvm/gems/ruby-2.4.1/bin:/Users/majialun/.rvm/gems/ruby-2.4.1@global/bin:/Users/majialun/.rvm/rubies/ruby-2.4.1/bin:/Users/majialun/.rvm/bin:/Users/majialun/Documents/flutter/flutter/bin

这个时候我们发现太多了,但是不要慌,刚才安装Express的时候,有一个提示显示了npm的全局脚本挂在哪个path下。

再看看安装成功的提示:

/Users/majialun/.npm-global/bin/express -> /Users/majialun/.npm-global/lib/node_modules/express-generator/bin/express-cli.js
+ express-generator@4.16.1
updated 1 package in 2.123s

我们可以看到,脚本安装在/Users/majialun/.npm-global/bin,这个目录里就是全部的全局脚本,注意是bin目录,express只是这个目录下的一个而已。

然后我们仔细在全局的path里找,发现并不存在这个路径……我也不知道我是怎么把路径搞没的,我在公司的Macbook Pro一切正常,但是随身带的Macbook就缺失路径。

破案

我们直接去根目录(/Users/majialun/)下,显示隐藏文件后,看看有没有.bash_profile,没有就新建一个,有就直接修改,添加进去一个路径:

export PATH=$PATH:/Users/majialun/.npm-global/bin:$PATH

注意这个路径是从上面复制下来的,每个人的不一样,总之从安装成功的提示里,复制到/bin这里结束就可以了。
好了之后,在终端里打source,然后把.bash_profile 拖进来,就有了:

$ source /Users/majialun/.bash_profile 

执行一下然后重启终端,跑起来试试看:

majialun$ express

  warning: the default view engine will not be jade in future releases
  warning: use `--view=jade' or `--help' for additional options

destination is not empty, continue? [y/N] 

没有not found 的报错,问题解决。

本文转载需要经过作者同意,否则禁止任何转载。


马嘉伦
874 声望845 粉丝