【问题标题】:Unable to run babel command on CLI无法在 CLI 上运行 babel 命令
【发布时间】:2017-11-18 18:04:58
【问题描述】:

我正在尝试按照文档建议安装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 中” - 但是您认为在 $PATH 中添加所有项目的 node_modules/.bin 是否好?或者有什么替代方法吗?

PS:我在windows机器上。

【问题讨论】:

  • 哪个操作系统?尝试全局安装? npm install --global babel-cli
  • @mkaatman 我正在 Windows 上尝试,babeljs.io/docs/usage/cli docs 说“你不应该全局安装它”
  • 是的,但你在 Windows 上。所有赌注都取消了。
  • @mkaatman babel 在 Windows 上不起作用?
  • 试试:./node_modules/.bin/babel

标签: javascript babeljs flowtype


【解决方案1】:

在您的 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中指定的babel版本并由npm安装,你不需要编辑你的PATH变量。

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

【讨论】:

  • 我需要执行这个命令babel src/ -d lib/你能建议吗。我试过这个npm run babel src/ -d lib/,但这并没有在 lib 目录中生成输出文件。当我在环境路径中设置 babel 时,它就可以工作了
  • @Williams 请参阅上面关于传递参数的注释。您将需要npm run babel -- src/ -d lib/。对于它的价值,在大多数情况下,我可能会定义一个新脚本"compile": "babel src/ -d lib/",而不是采用传递参数的方法。
  • @NSharma ,请指定您如何“在环境路径中设置 babel”?
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 2016-02-07
  • 2018-06-18
相关资源
最近更新 更多