90

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?

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
Kokodoko
  • 26,167
  • 33
  • 120
  • 197

14 Answers14

99

There are two problems here. First, you need a package.json file. Telling npm to install without one will throw the npm WARN enoent ENOENT: no such file or directory error. In your project directory, run npm init to generate a package.json file for the project.

Second, local binaries probably aren't found because the local ./node_modules/.bin is not in $PATH. There are some solutions in How to use package installed locally in node_modules?, but it might be easier to just wrap your babel-cli commands in npm scripts. This works because npm run adds the output of npm bin (node_modules/.bin) to the PATH provided to scripts.

Here's a stripped-down example package.json which returns the locally installed babel-cli version:

{
  "scripts": {
    "babel-version": "babel --version"
  },
  "devDependencies": {
    "babel-cli": "^6.6.5"
  }
}

Call the script with this command: npm run babel-version.

Putting scripts in package.json is quite useful but often overlooked. Much more in the docs: How npm handles the "scripts" field

Community
  • 1
  • 1
joemaller
  • 19,579
  • 7
  • 67
  • 84
  • 2
    After considering the other answers, and exploring messing with my `~/.profile` file to ensure I can call the packages in the local `node_modules`, I've concluded the `scripts` area of `package.json` is the best option. You won't need to tell your co-workers to change their profiles or make alias, your scripts will just work. – Jared Egan Jul 05 '16 at 15:25
  • I'm using Yarn (work requirement) and most fixes I've seen for this issue use npx. The "./node_modules/.bin is not in $PATH" fixed it for me. Thanks! – Chris DiPiero Apr 22 '21 at 17:43
  • If you're running in docker, then after npm install, the files might not end up in the node_modules (known linux weirdness, UID/GID??). Then, ofc, the binaries aren't found in node_modules/bin. – JWCS May 01 '23 at 22:25
46

When I found this question, I was looking for

$ npm install -g babel-cli
JBaczuk
  • 13,886
  • 10
  • 58
  • 86
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 2
    You should never have to use sudo with npm. Try using nvm to manage your node versions. – John Franke Nov 17 '17 at 16:20
  • 1
    ```npm install -g babel-cli``` Will work without sudo. – John Franke Dec 01 '17 at 20:53
  • 3
    you should use `sudo` with `npm install -g babel-cli` to access this file /usr/local/lib/node_modules, else you'll be denied. – Ericgit Jul 20 '19 at 22:41
  • The @babel/cli docs recommends against installing babel-cli globally (to avoid conflicts with other projects using different versions of babel, and to improve portability) – 7hibault Jun 29 '20 at 10:23
17

Installing babel globally solves this issue:

npm install -g @babel/core @babel/cli

However, it is not encourage to install dependencies globally because they won't have their versions managed on a per-project basis.

You should install your dependencies locally, as suggested on babel's documentation:

npm install --save-dev @babel/core @babel/cli

The downside is that this gives you no fast/convenient way to invoke local binaries interactively (in this case babel). npx gives you a great solution:

npx babel --version

This will run your local installation of babel. Additionally, if you want to avoid typing npx, you can configure the shell auto fallback, and then just run:

babel --version

Note: it is important to create a file .babelrc, at your project's root, in which you specify your babel configuration. As a starting point you can use env-preset to transpile to ES2015+:

npm install @babel/preset-env --save-dev

In order to enable the preset you have to define it in your .babelrc file, like this:

{
  "presets": ["@babel/preset-env"]
}
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • As suggested by the [documentation](https://babeljs.io/docs/en/babel-cli#install) you need to install @babel/core and @babel/cli irrespective of their versions. – guneetgstar Mar 31 '21 at 20:52
12

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

Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
Alamelu Venkat
  • 471
  • 1
  • 4
  • 15
12

To install version 7+ of Babel run:

npm install -g @babel/cli
npm install -g @babel/core
sean2078
  • 5,131
  • 6
  • 32
  • 32
6

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

Yega
  • 356
  • 5
  • 10
3

I had the same issue. Deleted the nodemodules folder and opened command prompt as administrator and then ran npm install.

All packages installed fine.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Madhav
  • 41
  • 1
2

Worked for me e.g.

./node_modules/.bin/babel --version
./node_modules/.bin/babel src/main.js
Mandakini
  • 31
  • 1
1

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

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

Mahtab Alam
  • 1,810
  • 3
  • 23
  • 40
1

Actually, if you want to use cmd commands,you have two ways. First, install it at gloabl environment. The other way is npm link. so, try the first way: npm install -g babel-cli.

YinPeng.Wei
  • 552
  • 3
  • 9
1

One option is to install the cli globally.

Since Babel 7 was released the namespace has changed from babel-cli to @babel/cli, hence:

npm install --global @babel/cli

You'll likely still encounter errors for @babel/core so:

npm install --global @babel/core
kylegill
  • 314
  • 1
  • 12
0

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...

bakke2ooo
  • 1
  • 2
0

For those using Yarn as their package manager instead of npm:

yarn global add babel-cli
Cody Pinto
  • 101
  • 1
  • 6
0

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"
ariel guzman
  • 590
  • 7
  • 11