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?
For those using Yarn as their package manager instead of npm:
Installing
babel
globally solves this issue: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: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:This will run your local installation of
babel
. Additionally, if you want to avoid typingnpx
, you can configure the shell auto fallback, and then just run: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 useenv-preset
to transpile to ES2015+:In order to enable the preset you have to define it in your .babelrc file, like this:
There are two problems here. First, you need a
package.json
file. Telling npm to install without one will throw thenpm WARN enoent ENOENT: no such file or directory
error. In your project directory, runnpm init
to generate apackage.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 becausenpm run
adds the output ofnpm bin
(node_modules/.bin
) to thePATH
provided to scripts.Here's a stripped-down example
package.json
which returns the locally installed babel-cli version: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
Worked for me e.g.
I had the same issue. Deleted the
nodemodules
folder and opened command prompt as administrator and then rannpm install
.All packages installed fine.
One option is to install the cli globally.
Since Babel 7 was released the namespace has changed from
babel-cli
to@babel/cli
, hence:You'll likely still encounter errors for
@babel/core
so: