Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel command not found

Tags:

npm

babeljs

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?

like image 406
Kokodoko Avatar asked Dec 22 '15 17:12

Kokodoko


People also ask

Can not find Babel core?

To solve the error "Cannot find module '@babel/core'", make sure to install the @babe/core package by opening your terminal in your project's root directory and running the following command: npm i -D @babel/core and restart your IDE and development server.

How do I know if Babel is installed?

You can also check the version of babel-cli by finding the babel-cli folder in node_modules and looking at the version property of the package. json that is at the base of that folder. If babel-cli was installed globally via -g flag of npm install , you could check the version by executing command babel --version .


1 Answers

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

like image 54
joemaller Avatar answered Oct 15 '22 11:10

joemaller