错误:在Function.Module._resolveFilename中找不到模块'@ babel / core'(内部/模块/cjs/loader.js:582:15)

时间:2019-05-14 04:27:04

标签: node.js express babel

我正在尝试制作一个角度项目的后端。我是node.jsexpress.js的新手。我正在使用visual studio code作为该项目的文本编辑器。同时发生以下错误。我该怎么办。

Error: Cannot find module '@babel/core'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
PS C:\Users\ruwan\Documents\projects\new project\Hair-4-U-hospital\MEAN\backend> npm run dev

> backend@1.0.0 dev C:\Users\ruwan\Documents\projects\new project\Hair-4-U-hospital\MEAN\backend
> babel-watch server.js

internal/modules/cjs/loader.js:584
    throw err;
    ^

Error: Cannot find module '@babel/core'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\ruwan\Documents\projects\new project\Hair-4-U-hospital\MEAN\backend\node_modules\babel-watch\babel-watch.js:7:15)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! backend@1.0.0 dev: `babel-watch server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the backend@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\ruwan\AppData\Roaming\npm-cache\_logs\2019-05-14T04_01_56_313Z-debug.log

server.js

import express from 'express';
const app = express();
app.get('/', (req, res) => res.send('hellow world'));
app.listen(4000, () => console.log(`expresss server running on 4000`));

我的package.json文件如下。

package.json

{
    "name": "backend",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "babel-watch server.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "babel-cli": "^6.26.0",
        "babel-preset-env": "^1.7.0",
        "babel-watch": "^7.0.0"
    },
    "dependencies": {
        "cors": "^2.8.5",
        "express": "^4.16.4",
        "mongoose": "^5.5.8"
    }
}

.barbelrc

{
  "presets": ["env"]
}

为此,我使用了以下命令。比我该怎么办。

npm init -y

npm install --save-dev babel-cli babel-preset-env

npm install babel-watch --save-dev

npm install express

npm install mongoose

npm install cors

npm run dev

2 个答案:

答案 0 :(得分:2)

安装babel-core。运行npm install -D @babel/core

由于这是一个Node.js项目,因此安装babel-node也是一个好主意。通过运行npm i -D @babel/node

为什么不仅仅使用babel 7或babel 6而不是对不同的babel软件包使用不同的版本?

答案 1 :(得分:0)

仍然遇到同样的问题

Module build failed: Error: Cannot find module 'babel-core'
 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)

共享我的package.json

"dependencies": 
{
    "babel-loader": "^7.1.5",

    "cli-color": "^2.0.0",
    "colors": "^1.3.3",
    "diff": "^4.0.1",
    "file-loader": "^4.2.0"}
devDependencies": 
{
    "@babel/cli": "^7.6.4",

    "@babel/core": "^7.6.4",
    "@babel/plugin-proposal-class-properties": "^7.5.5",
    "@babel/preset-env": "^7.6.3",
    "@babel/preset-react": "^7.6.3",
    "babel-eslint": "^8.2.2",
    "babel-minify": "^0.3.0",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "babel-register": "^6.24.1",
    "cross-env": "^6.0.3",
    "css-loader": "^3.2.0",
    "del": "^5.1.0",
    "eslint": "^4.18.2",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-plugin-import": "^2.9.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.7.0",
    "gulp": "^3.9.1",
    "gulp-awspublish": "^3.3.1",
    "gulp-babel": "^7.0.1",
    "gulp-bump": "^3.1.3",
    "gulp-git": "^2.9.0",
    "gulp-if": "^2.0.2",
    "gulp-prompt": "^1.2.0",
    "gulp-rename": "^1.2.2",
    "json-loader": "^0.5.7",
    "modernizr-loader": "^1.0.1",
    "node-sass": "^4.7.2",
    "path": "^0.12.7",
    "prettier-eslint": "^8.8.2",
    "react-hot-loader": "^4.0.0",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.0",
    "webpack-cli": "^3.3.9"
  },