简体   繁体   中英

Error: Cannot find module 'babel-register'

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.

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.

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"
  }

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:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM