Javascript 无法通过 npm 脚本“babel: command not found”运行 babel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36106817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to run babel via npm script "babel: command not found"
提问by Justin
To get started I ran:
首先,我跑了:
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0
Here is my package.json:
这是我的 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"
}
}
Here is my .babelrc file:
这是我的 .babelrc 文件:
{
"presets": ["es2015", "stage-0"]
}
My file structure is like this:
我的文件结构是这样的:
- Root
- src
- client
- server
- test
- dist
- package.json
I am calling npm run build from the root folder. I am expecting it to compile the source folder into the dist folder. It runs and then I get this error:
我从根文件夹调用 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! [email protected] build: `babel src -d dist`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] 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
So as you can see, I've installed babel-cli, I've installed the presets, and I think everything is in order according to the babel docs.
正如你所看到的,我已经安装了 babel-cli,我已经安装了预设,我认为一切都按照 babel 文档进行。
Does anyone have ideas about why it wouldn't be working? Could I be missing a babel npm file? Is "babel src -d dist" incorrect?
有没有人知道为什么它不起作用?我可能缺少 babel npm 文件吗?“babel src -d dist”不正确吗?
Thanks for any help you can provide!
感谢您的任何帮助,您可以提供!
I made another folder and followed the same steps, it worked perfectly. For some reason it's not working in this directory.
我制作了另一个文件夹并按照相同的步骤操作,效果很好。出于某种原因,它在此目录中不起作用。
采纳答案by s.xie
Did you run "npm install" to install the dev packages?
您是否运行“npm install”来安装开发包?
回答by tom
I've come across the same issue lately. Removing the node_modules folder and running npm install
again no longer fixes the issue.
我最近遇到了同样的问题。删除 node_modules 文件夹并npm install
再次运行不再解决问题。
The reason you are getting this error is because babel-cli needs to be installed globally, not as a project dependency.
您收到此错误的原因是需要全局安装 babel-cli,而不是作为项目依赖项。
Run npm install -g babel-cli
to install it globally.
运行npm install -g babel-cli
以全局安装它。
Babel-preset-es2015 can then be installed as a dev dependency for your projects npm install --save-dev babel-preset-es2015
然后可以将 Babel-preset-es2015 安装为项目的开发依赖项 npm install --save-dev babel-preset-es2015
回答by dylanh724
You should never install babel-cli globally - in fact, they specifically have an entire paragraph telling you not tofrom their official docs.
你永远不应该全局安装 babel-cli - 事实上,他们专门有一整段告诉你不要从他们的官方文档中找到。
Edit package.json
>> add a script with the key called, say, build
with the value ./node_modules/.bin/babel <commands>
编辑package.json
>> 添加一个带有键的脚本,例如,build
带有值./node_modules/.bin/babel <commands>
If you called it build
, just then type npm run build
.
如果您调用它build
,只需键入npm run build
。
回答by gnerkus
The error occurs because ./node_modules/.bin
is not in $PATH
. ./node_modules/.bin
is where all the executable binaries can be found.
发生错误是因为./node_modules/.bin
不在$PATH
. ./node_modules/.bin
可以找到所有可执行的二进制文件。
As recommended by the documentation, you can reference the babel
cli inside of node_modules
:
按照文档的建议,您可以参考以下内容中的babel
cli node_modules
:
$ ./node_modules/.bin/babel src -d lib
You can modify your npm run build
command to use this:
您可以修改您的npm run build
命令以使用它:
"scripts": {
"build": "./node_modules/.bin/babel src -d dist"
},
回答by Ahmad Awais
Many of the answers above are correct.
上面的许多答案都是正确的。
The error occurs because ./node_modules/.bin is not in
$PATH
../node_modules/.bin
is where all the executable binaries can be found.
发生错误是因为 ./node_modules/.bin 不在
$PATH
../node_modules/.bin
可以找到所有可执行的二进制文件。
What I did was I built a simple dynamic alias function in my zshrc file.
我所做的是在我的 zshrc 文件中构建了一个简单的动态别名函数。
# Babel
function bbl() {
./node_modules/.bin/babel "$@"
}
Now you can use bbl
instead of babel
现在你可以使用bbl
代替 babel
bbl --version
6.24.1 (babel-core 6.25.0)