3

I am trying to setup a simple express server. I am using nodemon to start my development server But my app keeps crashing because it does not recognize the "babel-node" command.

The error output is

[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,json
[nodemon] starting `babel-node index.js`
'babel-node' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...

my package.json scripts are

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "startdev": "nodemon --ext js,json  --exec babel-node index.js",
    "start": "babel-node index.js"
  }

and my dependencies and dev dependencies are

"dependencies": {
    "express": "^4.17.1",
    "express-graphql": "^0.12.0",
    "graphql": "^15.4.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.12.10",
    "@babel/core": "^7.12.10",
    "@babel/node": "^7.12.10",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.1",
    "@babel/preset-env": "^7.12.11",
    "nodemon": "^2.0.7"
  }

I tried testing it without nodemon, by using the regular node command and it runs as expected


$ npm run start

> dev_forum@1.0.0 start
> babel-node index.js

Server is up...

My folder strucher is below

enter image description here

the contents of index.js are

const express = require("express");


const app = express();

app.listen(() => {
    console.log("Server is up...")
})

I have also tried deleting my node_modules and package-lock.json files and reinstalling but still crashes.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dagmawi Asfaw
  • 39
  • 1
  • 7

3 Answers3

6

Remove your node_modules and follow these steps:

 npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node

Then, check the existence of these files:

node_modules/.bin/babel-node

node_modules/.bin/babel-node.cmd - windows only

node_modules/@babel/node/bin/babel-node.js

If everything looks good add to package.json:

"start": "nodemon --exec babel-node index.js",
Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • I have done that but the error still persists... Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3".. so i solved it by reverting back to babel version 6 ... "devDependencies": { "babel-cli": "^6.26.0", "babel-core": "^6.26.3", "babel-node": "^0.0.1-security", "babel-preset-env": "^1.7.0", "nodemon": "^2.0.7" } – Dagmawi Asfaw Jan 22 '21 at 05:50
  • 1
    Still the same issue? Try reverting your nide version to 14 or try to install nvm and check if the issue is not specific to node version. – Apoorva Chikara Jan 22 '21 at 05:53
  • Previously i was using node v15 but used node 14.15.4 and i still get the same error.. But i have found that the problem was with my babel plugin and presets.. i was using the @babel/preset-env and @babel/plugin-proposal-object-rest-spread.. i tested each independently but still got the same error. i solved it by using the previous versions babel-preset-env and babel-plugin-transform-object-rest-spread package. These work with both babel v6 and v7 – Dagmawi Asfaw Jan 22 '21 at 06:16
  • Thats great! Yes, It looks like the version issues only as installation seems fine, but good to hear that you resolved it. – Apoorva Chikara Jan 22 '21 at 06:45
  • 1
    It didn't work for me when I used "npm start", but it is fine when I use "yarn start" – user1880531 Apr 26 '21 at 00:34
3

I ran into the same problem and solved it this way:

"scripts": {
    "start": "babel-node src/index.js",
    "dev"  : "nodemon --exec npm start"
  }

In the terminal run

npm run dev
hkiame
  • 167
  • 3
  • 7
Boris Torrejon
  • 116
  • 1
  • 1
1

None of the steps above work for me. I resorted to using yarn instead. Delete your package-lock.json Then do: yarn This will add the dependencies, then you can run it, most likely with yarn run dev (of course this depends on your scripts on package.json).

Daniel Tkach
  • 576
  • 2
  • 9
  • 18