14

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
  • 113,669
  • 10
  • 50
  • 103
Matt H.
  • 367
  • 1
  • 2
  • 7

6 Answers6

21

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.
  • 367
  • 1
  • 2
  • 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
  • 181
  • 2
  • 11
1

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

Ilya Chernomordik
  • 23,987
  • 20
  • 99
  • 170
  • 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
  • 51,262
  • 47
  • 169
  • 217
1

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.

First, here is my test script and 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, I also have two files:

babel.config.json

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

.mocharc.yaml

require:
    - "@babel/register"

The difference between running just mocha and npm "test": "mocha" is that npm test knows to look for mocharc.yaml. I got this hint by looking at 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
  • 86
  • 1
  • 5
0

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

user158
  • 11,548
  • 7
  • 56
  • 83