无法通过 npm 脚本“babel:找不到命令”运行 babel
Unable to run babel via npm script "babel: command not found"
为了开始,我运行了:
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0
这是我的包.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 运行构建。我希望它将源文件夹编译为 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
你永远不应该全局安装 babel-cli - 事实上,他们特别有一整段告诉你不要从他们的官方文档中安装。
编辑package.json
>>添加一个脚本,其中包含调用的键,例如,build
值为 ./node_modules/.bin/babel <commands>
如果您将其称为build
,则键入 npm run build
。
发生此错误是因为./node_modules/.bin
不在$PATH
中。 ./node_modules/.bin
可以找到所有可执行二进制文件的地方。
按照文档的建议,您可以在 node_modules
中引用 babel
cli:
$ ./node_modules/.bin/babel src -d lib
您可以修改 npm run build
命令以使用此命令:
"scripts": {
"build": "./node_modules/.bin/babel src -d dist"
},
您是否运行了"npm install"来安装开发包?
上面的许多答案都是正确的。
发生此错误的原因是 ./node_modules/.bin 不在
$PATH
中。./node_modules/.bin
可以找到所有可执行二进制文件的地方。
我所做的是在我的 zshrc 文件中构建一个简单的动态别名函数。
# Babel
function bbl() {
./node_modules/.bin/babel "$@"
}
现在你可以用bbl
代替 babel
bbl --version
6.24.1 (babel-core 6.25.0)
- 气质的“nestRemoting()”有时可以'找不到关系
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- 找不到模块捆绑包
- for循环中的javascript if语句找不到==
- 为什么JavaScript可以'找不到给定的InnerHTML并返回Cannot set property
- 失败:等待Protractor与页面同步时出错:“”;在窗口上找不到角度”;
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- [Vue warn]:找不到元素
- ngRoute找不到模板
- 未能加载,因为找不到支持的源.当播放HTML5音频元素时
- 因果报应-找不到模块:错误:无法解析模块'scs'
- Angular 2:在本地.json文件上找不到文件
- module.js在运行iron节点时找不到模块
- 放大弹出生成“;找不到文件”;YouTube视频的错误
- 我发现了一些只在移动设备上可见的垃圾邮件链接,我可以'找不到包含此垃圾邮件链接的脚本的位置
- Web 地址虚拟目录会断开外部 JavaScript 链接,因此找不到文件
- 无法通过 npm 脚本“babel:找不到命令”运行 babel
- "警告:任务;babel”;找不到.使用--force继续&”;
- 在Babel中导出es6类-找不到模块
- 找不到兼容的版本:babel-plugin-transform-decorators@^6.13.0