无法在CLI上运行babel命令

时间:2017-06-16 05:01:04

标签: javascript babeljs babel flowtype

我正在尝试安装flow,因为文档建议https://flow.org/en/docs/install/需要安装babel-cli才能运行babel

我使用以下命令https://babeljs.io/docs/usage/cli/

安装了babel-cli
  

npm install --save-dev babel-cli

> npm install --save-dev babel-cli
todo-app@0.1.0 D:\React JS\todo-app
`-- babel-cli@6.24.1

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.0.17 (node_modules\react-scripts\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.0.17: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN enoent ENOENT: no such file or directory, open 'D:\React JS\todo-app\node_modules\acorn-dynamic-import\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'D:\React JS\todo-app\node_modules\create-hmac\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'D:\React JS\todo-app\node_modules\create-hash\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'D:\React JS\todo-app\node_modules\diffie-hellman\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'D:\React JS\todo-app\node_modules\pbkdf2\package.json'

的package.json

{
  "name": "todo-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-redux": "^5.0.5",
    "react-tap-event-plugin": "^2.0.1",
    "redux": "^3.6.0"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "react-scripts": "1.0.7",
    "redux-devtools": "^3.4.0",
    "webpack": "^2.6.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

当我运行babel命令时,它会给我一个错误'babel'不被识别为内部或外部命令,可操作程序或批处理文件。

更新

这是因为“可能找不到本地二进制文件,因为本地./node_modules/.bin不在$ PATH中” - 但您认为在$中添加node_modules/.bin所有项目是否合适?路径?还是有其他选择吗?

PS:我在Windows机器上。

1 个答案:

答案 0 :(得分:4)

package.json

中添加babel脚本
"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject",
  "babel": "babel"
}

然后你可以用以下方式运行babel:

npm run babel

如果要在从命令行运行时传递参数,请将它们与-- (more info)的命令分开。例如:

npm run babel -- --help

这样您运行package.json中指定并由npm安装的babel版本,您无需编辑PATH变量。

注意:这是same approach as flow recommends for itself in its installation instructions,。 (将其切换到顶部的npm,然后向下滚动到"设置流程")