5

Issue:

Unable to use Babel Transpiler with Nodemon

Details:

In package.json I have:

"scripts": {
   "start": "nodemon --exec babel-node --presets=es2015 -- src/app.js"
},


"dependencies": {
   "nodemon": "^1.18.4"
},
"devDependencies": {
  "@babel/cli": "^7.1.2",
  "@babel/core": "^7.1.2",
  "@babel/preset-env": "^7.1.0",
  "babel-cli": "^6.26.0",
  "babel-preset-env": "^1.7.0"
}

When I run npm start My understanding is that nodemon should kick on save and run the babel transpiler; however, I get the following in the terminal.

Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

I thought this was caused by the babel-cli dependency of 6.26 but when I remove that it squawks:

[nodemon] failed to start process, "babel-node" exec not found

Searching through the good ol' google machine I see some other people with more complicated setups and their solutions seem to fly over the top of my head.

Recreation Steps:

  1. Run npm init -y & npm i nodemon

  2. Follow instructions here: https://babeljs.io/setup#installation (nodemon) selected

  3. Run npm install @babel/core --save-dev as I was warned that the core was not installed.

  4. Run npm start

1 Answers1

9

When running Babel with nodemon you need to include these packages.

"devDependencies": {
  "@babel/cli": "^7.1.2",
  "@babel/core": "^7.1.2",
  "@babel/polyfill": "^7.0.0",
  "@babel/preset-env": "^7.1.0",
  "@babel/node": "^7.0.0",
  "nodemon": "^1.18.4"
}

Then adjust your npm run script to:

"start": "nodemon app/index.js --exec babel-node app/index.js"

Thanks to the Babel Slack channel for this answer!

  • 3
    After searching through multiple github issues and thread here, this is the only solution that worked. In my case After pasting this, I had to delete package-lock.json file and node modules folder and run npm install again to delete all traces of previous node module files. – Nabil Mohammed Nalakath Sep 19 '19 at 12:16
  • As of Babel 7.4.0, `@babel/polyfill` package has been deprecated in favor of directly including `core-js/stable` via https://babeljs.io/docs/en/babel-polyfill – DeBraid Apr 07 '21 at 19:55