1

I installed Babel locally in my project with the following command:

npm install babel-cli babel-core babel-preset-es2015 --save-dev

This gives me the following output in package.json:

{
  "name": "my_project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "babel src -d lib"
  },
  "scripts": {
    "build": "babel --presets es2015 src -d lib"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-cli": "^6.18.0",
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.8",
    "babel-preset-es2015": "^6.18.0",
    "webpack": "^1.13.3"
  },
  "dependencies": {
    "lodash": "^4.17.2"
  }
}

I'm not sure, but it looks like it was installed successfully. But when I try to run npm run babel I get:

npm ERR! Darwin 15.3.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "babel"
npm ERR! node v7.0.0
npm ERR! npm  v4.0.2

npm ERR! missing script: babel
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
nam ERR!     /Path/To/My/Project/npm-debug.log

Why doesn't Babel work? In the package.json file it is included:

3

2 Answers 2

3

So you run npm run babel

and it said npm ERR! missing script: babel

may be you meant this: npm run build


Two things to ponder:

  1. npm run

    This runs an arbitrary command from a package's "scripts" object.

  2. babel is not a script in your package.json, but a transpiler for JavaScript which converts ES6 to ES5.

so if you run npm run build, that will execute the build command defined here:

  "scripts": {
    "build": "babel src -d lib"
  },

I also see there is a duplicate of scripts object.

0
1

There are different ways to run an installed npm executables.

The easiest way is to install it also globally. In your case you need only babel-cli to be available in command line. so npm install -g babel-cli will do.

If you want to install it only locally, babel executable is also available on node_modules/.bin/babel. But surely it's much of typing for a command.

The other way that you are trying is to use npm-run-scripts. In this case you have to define the script in your package.json. You already have a build script that you can run like npm run build. So for npm run babel you need the same form of script definition:

"scripts": {
    "build": "babel src -d lib",
    "babel": "babel script.js --out-file script-compiled.js" // For example
}
...

This is used mostly to have a shorthand for a command that you want to run frequently like a build script.

You can also provide additional arguments to an npm script with this syntax:

npm run babel -- --presets es2015

Notice the additional -- here.

There is still another option where you can install a package once globally and link to it from different projects:

npm install -g babel-cli and in your project direcotory npm link babel-cli.

The most common way is the easiest one where you install executable packages (command line interfaces) both globally and locally.

Not the answer you're looking for? Browse other questions tagged or ask your own question.