Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run babel via npm script "babel: command not found"

Tags:

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:

   {
      "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:

{
  "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:

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

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?

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.

like image 944
Justin Avatar asked Mar 19 '16 20:03

Justin


People also ask

Why does NPM install fail to install Babel?

Removing the node_modules folder and running npm install again no longer fixes the issue. The reason you are getting this error is because babel-cli needs to be installed globally, not as a project dependency. Run npm install -g babel-cli to install it globally.

Why is my Babel script not working in Babel 6?

scjs ❯ babel script.js You have mistakenly installed the `babel` package, which is a no-op in Babel 6. Babel's CLI commands have been moved from the `babel` package to the `babel-cli` package. npm uninstall babel npm install babel-cli

How do I fix the Babel command not found error?

Use npx to solve the error "babel: command not found", e.g. npx --package @babel/cli babel --version or install the package globally by running npm install -g @babel/cli to be able to use the command without the npx prefix. The fastest way to solve the error is to use the npx command with the --package flag.

How to use Babel in Node JS?

When you install locally, you can only use babel directly if you have it in your PATH. You'll need to do ./node_modules/.bin/babel, $ (npm bin)/babel or call it from a package.json "scripts" command.


2 Answers

I've come across the same issue lately. Removing the node_modules folder and running npm install again no longer fixes the issue.

The reason you are getting this error is because babel-cli needs to be installed globally, not as a project dependency.

Run npm install -g babel-cli to install it globally.

Babel-preset-es2015 can then be installed as a dev dependency for your projects npm install --save-dev babel-preset-es2015

like image 94
tom Avatar answered Sep 21 '22 19:09

tom


You should never install babel-cli globally - in fact, they specifically have an entire paragraph telling you not to from their official docs.

Edit package.json >> add a script with the key called, say, build with the value ./node_modules/.bin/babel <commands>

If you called it build, just then type npm run build.

like image 37
dylanh724 Avatar answered Sep 18 '22 19:09

dylanh724