简体   繁体   English

无法在CLI上运行babel命令

[英]Unable to run babel command on CLI

I am trying to install flow as docs suggests https://flow.org/en/docs/install/ It requires to install babel-cli to run babel . 我正在尝试按照文档建议https://flow.org/en/docs/install/安装flow ,它需要安装babel-cli才能运行babel

I installed babel-cli using below command https://babeljs.io/docs/usage/cli/ 我使用下面的命令https://babeljs.io/docs/usage/cli/安装了babel-cli

npm install --save-dev 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 的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"
  }
}

When I run babel command then it gives me an error 'babel' is not recognized as an internal or external command, operable program or batch file. 当我运行babel命令时,它给我一个错误“ babel”,它不被识别为内部或外部命令,可操作程序或批处理文件。

Update 更新

It is because "local binaries probably aren't found because the local ./node_modules/.bin is not in $PATH" - but do you think Is it good to add node_modules/.bin of all projects in a $PATH ? 这是因为“因为本地./node_modules/.bin不在$ PATH中,所以可能找不到本地二进制文件”-但是您认为在$ PATH中添加所有项目的node_modules/.bin是否很好? or Is there any alternative to do ? 还是有其他选择吗?

PS: I am on windows machine. PS:我在Windows计算机上。

Add a babel script in your package.json 在您的package.json中添加babel脚本

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject",
  "babel": "babel"
}

Then you can run babel with: 然后,您可以使用以下命令运行babel:

npm run babel

If you want to pass arguments when running from the command line, separate them from the command with -- (more info) . 如果要在从命令行运行时传递参数,请使用-- (更多信息)将其与命令分开。 For example: 例如:

npm run babel -- --help

This way you run the version of babel specified in package.json and installed by npm and you do not need to edit your PATH variable. 这样,您可以运行在package.json指定并由npm安装的babel版本,而无需编辑PATH变量。

Note: This is the same approach as flow recommends for itself in its installation instructions, . 注意:这flow在其安装说明中建议的方法相同 (Switch it to npm at the top then scroll down to "Setup Flow") (将其切换到顶部的npm ,然后向下滚动到“设置流程”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM