22

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! redacted@1.0.0 build: `babel src -d dist`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the redacted@1.0.0 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.

twernt
  • 20,271
  • 5
  • 32
  • 41
Justin
  • 2,265
  • 4
  • 15
  • 21

5 Answers5

17

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

tom
  • 383
  • 4
  • 7
  • 3
    This will get the command working, the only caveat is that the [Babel CLI docs](https://babeljs.io/docs/usage/cli/) strongly recommend that you don't install globally, even going so far as to suggest running `npm uninstall --global babel-cli` if you do have it installed globally. In their words this is because: _**1.** Different projects on the same machine can depend on different versions of Babel allowing you to update one at a time. **2.** It means you do not have an implicit dependency on the environment you are working in. Making your project far more portable and easier to setup._ – Joe Apr 06 '17 at 08:25
  • It isn't a good practice to use a global installation of `babel-cli`, besides removing the `node_modules` folder and running `npm install` **still** fixes the issue – Divyanshu Maithani Nov 27 '17 at 13:20
13

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.

dylanh724
  • 948
  • 1
  • 12
  • 29
9

The error occurs because ./node_modules/.bin is not in $PATH. ./node_modules/.bin is where all the executable binaries can be found.

As recommended by the documentation, you can reference the babel cli inside of node_modules:

$ ./node_modules/.bin/babel src -d lib

You can modify your npm run build command to use this:

"scripts": {
    "build": "./node_modules/.bin/babel src -d dist"
},
gnerkus
  • 11,357
  • 6
  • 47
  • 71
  • It doesn't need to be in $PATH when running command "npm run build" which is using the local copy. – s.xie Mar 19 '16 at 21:08
  • I tried this and I actually still receive the exact same error. I looked inside the bin folder and there is nothing in there with the name "babel" – Justin Mar 19 '16 at 21:17
6

Did you run "npm install" to install the dev packages?

s.xie
  • 159
  • 4
  • 1
    yes I have! For each one I did npm install and used --save-dev – Justin Mar 19 '16 at 21:15
  • 1
    Could you list the node_modules folder? And the ./node_modules/.bin? ls -l node_modules and ls -l ./node_modules/.bin – s.xie Mar 19 '16 at 22:00
  • Yep, it's a pretty large list so I put it on pastebin: http://pastebin.com/WVkWnixf @s.xie – Justin Mar 19 '16 at 22:11
  • Looks like you don't have a babel lnk file under ./node_module/.bin/ babel -> ../babel-cli/bin/babel.js* – s.xie Mar 19 '16 at 23:00
  • You can either create the symbol link: ln -s ./node_modules/.bin/babel ./node_modules/babel-cli/bin/babel.js or remove node_modules folder and run npm install command again. – s.xie Mar 19 '16 at 23:04
  • 1
    Removing node modules folder an reinstalling everything worked.. though I'm not sure why? Thanks so much for your help. – Justin Mar 20 '16 at 00:02
  • If removing the node_modules folder and reinstalling doesn't work, check what version of node you're using `node --version` if it's <= v2 then upgrade node and try again. Hint: Babel takes a long time to install on node v2 for some reason. – a2k42 Oct 20 '16 at 14:48
0

Many of the answers above are correct.

The error occurs because ./node_modules/.bin is not in $PATH. ./node_modules/.bin is where all the executable binaries can be found.

What I did was I built a simple dynamic alias function in my zshrc file.

# Babel
function bbl() {
    ./node_modules/.bin/babel "$@"
}

Now you can use bbl instead of babel

bbl --version
6.24.1 (babel-core 6.25.0)
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56