简体   繁体   English

错误:找不到模块“babel-register”

[英]Error: Cannot find module 'babel-register'

I am currently working on a projects that involves making an oracle connecting to an express back-end.我目前正在从事一个项目,该项目涉及使 oracle 连接到 express 后端。 The environment was already implemented and pushed to a repository on github.环境已经实现并推送到 github 上的存储库。 I cloned the project and ran npm install to get all the packages needed for the project.我克隆了项目并运行 npm install 以获取项目所需的所有包。 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.然后我继续 npm install babel-register 想也许这个包进入了 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.我删除了 package-lock 文件并安装了 npm。 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:我不得不通过删除 babel-register 中的引号来编辑我的脚本,正确的语句是:

"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:运行mocha返回此错误:

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

To fix it, I needed to run npm test instead of mocha .要修复它,我需要运行npm test而不是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:在与package.json相同的位置,我还有两个文件:

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 .只运行mochanpm "test": "mocha"之间的区别是npm test知道寻找mocharc.yaml I got this hint by looking at the Babel package in the mochajs/mocha-examples repository on GitHub.我通过查看 GitHub 上mochajs/mocha-examples存储库中的 Babel 包得到了这个提示。

npm test - run the tests using the local.mocharc.yaml config file npm test - 使用 local.mocharc.yaml 配置文件运行测试

Source:资源:

In my case I did not have a devDependency to babel-cli .就我而言,我没有对babel-clidevDependency Adding it made everything work fine添加它使一切正常

https://github.com/babel/babel/issues/10777#issuecomment-559765256 solved my issue: https://github.com/babel/babel/issues/10777#issuecomment-559765256解决了我的问题:

Issue was that mocha was in the global installed modules, and not part of dev dependencies问题是 mocha 在全局安装的模块中,而不是开发依赖项的一部分

This error could occur due to the mistake of not doing yarn install after cloning the repo.这个错误可能是由于在克隆 repo 后没有进行yarn install的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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