无法通过npm脚本运行babel"babel:command not found"

Jus*_*tin 17 javascript node.js npm babeljs

为了开始,我跑了:

npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0 
Run Code Online (Sandbox Code Playgroud)

这是我的package.json:

   {
      "scripts": {
        "build": "babel src -d dist"
      },
      "devDependencies": {
        "babel-cli": "^6.6.5",
        "babel-core": "^6.7.2",
        "babel-preset-es2015": "^6.6.0",
        "babel-preset-stage-0": "^6.5.0"
      }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的.babelrc文件:

{
  "presets": ["es2015", "stage-0"]
}
Run Code Online (Sandbox Code Playgroud)

我的文件结构是这样的:

- Root
    - src
        - client 
        - server
        - test  
    - dist 
    - package.json
Run Code Online (Sandbox Code Playgroud)

我从根文件夹调用npm run build.我期待它将源文件夹编译到dist文件夹中.它运行然后我收到此错误:

> babel src -d dist

sh: babel: command not found

npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build"
npm ERR! node v5.8.0
npm ERR! npm  v3.7.3
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! redacted@1.0.0 build: `babel src -d dist`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the redacted@1.0.0 build script 'babel src -d dist'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the redacted package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     babel src -d dist
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs redacted
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls redacted
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/user/redacted/npm-debug.log
Run Code Online (Sandbox Code Playgroud)

所以你可以看到,我已经安装了babel-cli,我已经安装了预设,我认为根据babel文档的所有内容都是有序的.

有没有人有关于它为什么不起作用的想法?我可以错过一个babel npm文件吗?"babel src -d dist"是不正确的?

感谢您的任何帮助,您可以提供!

我做了另一个文件夹并按照相同的步骤,它完美地工作.由于某种原因,它不在此目录中.

小智 13

我最近遇到过同样的问题.删除node_modules文件夹并npm install再次运行不再解决问题.

您收到此错误的原因是因为babel-cli需要全局安装,而不是作为项目依赖项.

运行npm install -g babel-cli以全局安装它.

然后可以将Babel-preset-es2015安装为项目的dev依赖项 npm install --save-dev babel-preset-es2015

  • 这将使命令正常工作,唯一需要注意的是[Babel CLI docs](https://babeljs.io/docs/usage/cli/)强烈建议您不要全局安装,甚至要到目前为止建议运行`npm uninstall --global babel-cli`如果你确实在全球安装了它.用他们的话说这是因为:_**1.**同一台机器上的不同项目可能依赖于不同版本的Babel,允许您一次更新一个.**2.**这意味着您没有对您正在使用的环境的隐式依赖.使您的项目更加便携,更容易设置._ (2认同)

dyl*_*724 10

你永远不应该在全球范围内安装babel-cli - 事实上,他们特别有一个段落告诉你不要从他们的官方文档中.

编辑package.json>>添加一个脚本,其中包含名为,例如,build带有值的键./node_modules/.bin/babel <commands>

如果您调用它build,则输入npm run build.


gne*_*kus 7

发生错误是因为./node_modules/.bin不在$PATH../node_modules/.bin是可以找到所有可执行二进制文件的地方.

根据文档的建议,您可以参考以下babel内容node_modules:

$ ./node_modules/.bin/babel src -d lib
Run Code Online (Sandbox Code Playgroud)

您可以修改npm run build命令以使用它:

"scripts": {
    "build": "./node_modules/.bin/babel src -d dist"
},
Run Code Online (Sandbox Code Playgroud)


小智 5

您是否运行“npm install”来安装开发包?