Babel command not found

2020-02-17 03:56发布

I have installed the babel-cli tool as explained by the Babel 'getting started' page.

From a terminal inside my project folder:

npm install --save-dev babel-cli

After this, there is a node_modules directory with a babel-cli folder, but there is no package.json created. npm also shows the following error:

npm WARN enoent ENOENT: no such file or directory, open '/Users/MyName/Sites/Tutorials/Babel2/package.json

When trying to run babel, I get this:

babel src -d lib
-bash: babel: command not found

I have the latest version of nodejs/npm installed. I have run npm update -g, and I have edited my .bash_profile file to include:

export PATH=$PATH:/Users/MyName/npm/bin
export PATH=/usr/local/share/npm/bin:$PATH

I have not experienced this with other npm tools such as browserify. Why is babel not recognized?

标签: npm babeljs
14条回答
Explosion°爆炸
2楼-- · 2020-02-17 04:38

You will need to add quotes around the path to your babel file as below

"./node_modules/.bin/babel" --help

查看更多
你好瞎i
3楼-- · 2020-02-17 04:39

This worked for me inside package.json as an npm script but it does seem to take to long grabbing the packages even though I have them as dev dependancies. It also seems too long.

"babel": "npx -p @babel/cli -p @babel/core babel --version"

What end up solving it was much simpler but funny too

npm install

I thought I ran that already but I guess somethings needed to be rebuilt. Then just:

"babel": "babel --version"
查看更多
叛逆
4楼-- · 2020-02-17 04:40

This is common issue and its looking for .cmd file from your root directory where you installed babel-cli. Try the below command.

./node_modules/.bin/babel.cmd

Once you are able to see your source code in the command prompt. Your next step is to install one more npm module babel-preset-es2015.

Follow the below answer to install babel-preset-es2015 and see why babel need this.

babel-file-is-copied-without-being-transformed

查看更多
霸刀☆藐视天下
5楼-- · 2020-02-17 04:43

This is what I've done to automatically add my local project node_modules/.bin path to PATH. In ~/.profile I added:

if [ -d "$PWD/node_modules/.bin" ]; then 
    PATH="$PWD/node_modules/.bin"
fi

Then reload your bash profile: source ~/.profile

查看更多
乱世女痞
6楼-- · 2020-02-17 04:46

When I found this question, I was looking for

$ npm install -g babel-cli
查看更多
手持菜刀,她持情操
7楼-- · 2020-02-17 04:47

I ran into the very same problem, tried out really everything that I could think of. Not being a fan of installing anything globally, but eventually had to run npm install -g babel-cli, which solved my problem. Maybe not the answer, but definitely a possible solution...

查看更多
登录 后发表回答