简体   繁体   English

反应,wepack,babel,节点,npm启动错误

[英]react, wepack, babel, node, npm start error

ERROR in ./main.js ./main.js中的错误

Module build failed (from ./node_modules/babel-loader/lib/index.js): 模块构建失败(来自./node_modules/babel-loader/lib/index.js):

Error: Cannot find module '@babel/preset-es2015' from 'F:\\reactapp' 错误:无法从“ F:\\ reactapp”中找到模块“ @ babel / preset-es2015”
at Function.module.exports [as sync] (F:\\reactapp\\node_modules\\resolve\\lib\\sync.js:43:15) 在Function.module.exports [作为同步](F:\\ reactapp \\ node_modules \\ resolve \\ lib \\ sync.js:43:15)
at resolveStandardizedName (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\files\\plugins.js:101:31) 在resolveStandardizedName(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ files \\ plugins.js:101:31)
at resolvePreset (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\files\\plugins.js:58:10) 在resolvePreset(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ files \\ plugins.js:58:10)
at loadPreset (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\files\\plugins.js:77:20) 在loadPreset(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ files \\ plugins.js:77:20)
at createDescriptor (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\config-descriptors.js:154:9) 在createDescriptor(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ config-descriptors.js:154:9)
at items.map (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\config-descriptors.js:109:50) 在items.map(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ config-descriptors.js:109:50)
at Array.map () at createDescriptors (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\config-descriptors.js:109:29) 在Array.map()在createDescriptors(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ config-descriptors.js:109:29)
at createPresetDescriptors (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\config-descriptors.js:101:10) 在createPresetDescriptors(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ config-descriptors.js:101:10)
at passPerPreset (F:\\reactapp\\node_modules@babel\\core\\lib\\config\\config-descriptors.js:58:96) 在passPerPreset(F:\\ reactapp \\ node_modules @ babel \\ core \\ lib \\ config \\ config-descriptors.js:58:96)
@ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./main.js main[2] @ multi(webpack)-dev-server / client?http:// localhost:8080(webpack)/hot/dev-server.js ./main.js main [2]

Child html-webpack-plugin for "index.html": 子html-webpack-plugin的“ index.html”:
1 asset 1项资产
Entrypoint undefined = index.html 入口点未定义= index.html
[./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 448 bytes {0} [built] [./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 448字节{0} [内置]
[./node_modules/lodash/lodash.js] 527 KiB {0} [built] [./node_modules/lodash/lodash.js] 527 KiB {0} [内置]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built] [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built] [./node_modules/webpack/buildin/global.js](webpack)/buildin/global.js 472字节{0} [内置] [./node_modules/webpack/buildin/module.js](webpack)/ buildin / module .js 497字节{0} [内置]

i ?wdm?: Failed to compile. 我?wdm ?:编译失败。

Terminate batch job (Y/N)? 终止批处理作业(是/否)?

package.json file: package.json文件:

package json file is as follows. 打包json文件如下。 I followed https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm 我关注了https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm

{
  "name": "reactapp",
  "version": "1.0.0",
  "description": "demo project",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "keywords": [
    "[]"
  ],
  "author": "manjunathan g",
  "license": "ISC",
  "dependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/preset-react": "^7.0.0",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "webpack": "^4.28.2",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.14"
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.2.3",
    "@babel/preset-env": "^7.2.3",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.4",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0"
  }
}

babel config: babel配置:

Babel config file is as below; Babel配置文件如下: followed as per https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm 按照https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm

{
  "presets":["env", "react"]
}

webpack config webpack配置

webpack config is as follows: webpack的配置如下:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
   entry: './main.js',
   output: {
      path: path.join(__dirname, '/bundle'),
      filename: 'index_bundle.js'
   },
   devServer: {
      inline: true,
      port: 8080
   },
   module: {
      rules: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
               presets: ['@babel/react', '@babel/es2015'],
               plugins: ['@babel/proposal-class-properties']
            }
         }
      ]
   },
   plugins:[
      new HtmlWebpackPlugin({
         template: './index.html'
      })
   ]
}

The @babel/preset-es2015 package has been deprecated and you can no longer install it from NPM. @babel/preset-es2015软件包已被弃用,您不能再从NPM安装它。

The recommendation now is to use @babel/preset-env instead. 现在的建议是改为使用@babel/preset-env

There are a lot of mistakes in your file configuration. 您的文件配置中有很多错误。 Let me try to solve it: 让我尝试解决它:

.babelrc .babelrc

You don't need the following devDependencies: babel-core , babel-preset-env , babel-preset-react and babel-preset-es2015 . 您不需要以下devDependencies: babel-corebabel-preset-envbabel-preset-reactbabel-preset-es2015 They are deprecated since Babel 7 was realeased. 自实现Babel 7以来,已弃用它们。 Substitute your code to this: 将您的代码替换为:

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

webpack.config.js webpack.config.js

Since webpack 4 was released, you don't need to inform the entry and output fields (You can do it for custom configurations). 由于webpack 4已发布,因此您无需通知输入和输出字段(您可以针对自定义配置进行操作)。 By default, webpack will look for index.js file located in the src/ directory (this directory must be in the root of your project). 默认情况下, webpack将在src/目录中查找index.js文件(此目录必须位于项目的根目录中)。 Webpack will create the module dependency graph from this file and output the bundled file to the dist/ directory. Webpack将根据该文件创建模块依赖关系图,并将捆绑的文件输出到dist/目录。 Try to configure your webpack.config.js like this: 尝试像这样配置webpack.config.js

const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")

module.exports = {
    module: {
        rules: [
            {
                test: /\.jsx$/,
                exclude: /node_modules/,
                use: { loader: "babel-loader" }
            },
            {
                test: /\.html$/,
                use: { loader: "html-loader" } //Install it: 'npm i -D html-loader'
            }
        ]
    },
    plugins: [
         new HtmlWebpackPlugin({
              template: "src/index.html" // Put the index.html in the src/ directory
         })
    ]
}

I'm not an expert in configuring webpack and I don't know if I could help you. 我不是配置Webpack的专家,也不知道是否能为您提供帮助。 I wrote an article on medium setting the environment to work with react, babel and webpack, but it is in portuguese . 我写了一篇有关在媒介设置环境以与React,Babel和Webpack一起工作的文章,但这是葡萄牙语 If you want to check: https://medium.com/@brunonakayabu/react-webpack-e-babel-configurando-o-ambiente-de-desenvolvimento-c7ee8a994222 如果要检查: https : //medium.com/@brunonakayabu/react-webpack-e-babel-configurando-o-ambiente-de-desenvolvimento-c7ee8a994222

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

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