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

标签 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 

这是我的 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"
      }
    }

这是我的 .babelrc 文件:

{
  "presets": ["es2015", "stage-0"]
}

我的文件结构是这样的:

- Root
    - src
        - client 
        - server
        - test  
    - dist 
    - package.json

我正在从根文件夹调用 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

如您所见,我已经安装了 babel-cli,我已经安装了预设,并且我认为根据 babel 文档,一切都井然有序。

有没有人知道为什么它不起作用?我会丢失 babel npm 文件吗? “babel src -d dist”不正确吗?

感谢您提供的任何帮助!

我创建了另一个文件夹并按照相同的步骤操作,效果很好。由于某种原因,它在此目录中不起作用。

最佳答案

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

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

运行 npm install -g babel-cli 全局安装。

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

关于javascript - 无法通过 npm 脚本运行 babel "babel: command not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36106817/

相关文章:

局部 View 中的 Javascript 如果局部 View 在页面上多次出现,如何让它只出现一次

Javascript 嵌套函数丢失作用域

node.js - Node.js 中的子进程 stdout 和 stderr 列大小

node.js - npm 安装和 Vagrant/Ansible 文件夹 chmod 问题

javascript - 如何在不安装 npm 的情况下包含第 3 方 npm 模块?

node.js - PhpStorm/WebStorm node_modules 和 package.json

javascript - 如何使用 Protractor 搜索类、在类中查找 p 标签并验证其某些文本?

javascript - 工厂内的$watch

node.js - 错误 : No valid exports main found for (Module path)

node.js - 我想使用 Node js 将 postman 文件转换为 openAPI 3.0