19

I am currently working on a projects that involves making an oracle connecting to an express back-end. The environment was already implemented and pushed to a repository on github. I cloned the project and ran npm install to get all the packages needed for the project. Then I tried to run the project and got this error:

module.js:550
   throw err;
   ^

Error: Cannot find module 'babel-register'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\xxxx\xxxx\Documents\Work\ef-backend\bin\www:1:63)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
[nodemon] app crashed - waiting for file changes before starting...

I then proceeded npm install babel-register thinking maybe the package made it into the gitignore. After the package was installed, I tried to run the project once more and continued to get the same error.

MT0
  • 143,790
  • 11
  • 59
  • 117
Matt H.
  • 447
  • 1
  • 3
  • 7

7 Answers7

24

I have resolved this issue myself, it was actually an issue with package-lock file being out of sync with the package file. I deleted the package-lock file and npm installed. This then allowed my project to run correctly.

Matt H.
  • 447
  • 1
  • 3
  • 7
5

in my case my script had an error:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node --require 'babel-register' src/index.js"
  }

I had to edit my script by removing quotes in babel-register, the correct statement was:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node --require babel-register src/index.js"
  }
EdwinCab
  • 361
  • 1
  • 3
  • 17
3

I recently had a similar issue; Running mocha was returning this error:

✖ ERROR: Error: Cannot find module '@babel/register'

To fix it, I needed to run npm test instead of mocha.

The npm test script is defined in the package.json manifest. Here are the relevant dependencies.

package.json

  "scripts": {
    ...
    "test": "mocha",
  },
  "devDependencies": {
    "@babel/cli": "^7.12.10",
    "@babel/core": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "@babel/register": "^7.12.10",
    "mocha": "^8.2.1"
  }

In the same location as package.json, there are 2 other files:

babel.config.json

{
    "presets": ["@babel/preset-env"]
}

.mocharc.yaml

require:
    - "@babel/register"

The difference between mocha and npm "test": "mocha" is that npm test uses the mocharc.yaml file. This hint came from the Babel package in the mochajs/mocha-examples repository on GitHub.

npm test - run the tests using the local .mocharc.yaml config file

Source:

Hannah
  • 106
  • 1
  • 6
  • 1
    This is a very complete and insightful answer. It links to the whole world of Mocha configurations, which I found very helpful for solving my slightly different problem. – Marco Faustinelli Feb 25 '23 at 16:43
1

In my case I did not have a devDependency to babel-cli. Adding it made everything work fine

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • for me it depended on the version, i did not need babel-cli until i installed the latest @babel/register, when i used an old version of it, it wasnt necessary – xunux Oct 01 '20 at 15:24
1

https://github.com/babel/babel/issues/10777#issuecomment-559765256 solved my issue:

Issue was that mocha was in the global installed modules, and not part of dev dependencies

Ben
  • 54,723
  • 49
  • 178
  • 224
1

This error could occur due to the mistake of not doing yarn install after cloning the repo.

user158
  • 12,852
  • 7
  • 62
  • 94
1
  1. If you guys are still facing issues please add production=false in .npmrc if your @babel/register is in dev dependency
  2. I am having the same issues with node 16 and 18
  3. Or use npm install --include=dev
  4. Solve by adding npm config set production false on groovy file
  5. if you do not want to do production false use npm config set include dev this will also work