简体   繁体   English

模块构建失败:TypeError:无法在Object.module.exports中读取未定义的属性“context”

[英]Module build failed: TypeError: Cannot read property 'context' of undefined at Object.module.exports

I got this unusual error after webpack installation, Searched whole web tried all solution but nothing works, 我在webpack安装后得到了这个不寻常的错误,搜索整个网络尝试了所有解决方案但没有任何作用,

//My webpack.config.js file //我的webpack.config.js文件

const webpack = require("webpack");
const path = require("path");
const config = {
    entry : path.resolve(__dirname,"src/index.js"),
    output : {
        path : path.resolve(__dirname,"dist/assets"),
        filename : "bundle.js",
        publicPath : "assets"
    },
    devServer : {
        inline : true,
        contentBase : path.resolve(__dirname,"dist"),
        port : 3000
    },
    module : {
        rules : [
            {
                test : /\.js$/,
                exclude : path.resolve(__dirname,"node_modules"),
                loader : "babel-loader",
                query: {
                    presets: ["env","latest","react","stage-0","es2015"]
                }
            },
            {
                test : /\.css$/,
                loader: 'style-loader!css-loader!autoprefixer-loader'
            },
            {
                test : /\.scss$/,
                loader: 'style-loader!css-loader!autoprefixer-loader!sass-loader'
            }
        ]
    }
};
module.exports = config;

My babelrc file 我的babelrc文件

{
  "presets" : ["env","latest","react","stage-0","es2015"]
}

Index.js file Index.js文件

import React from 'react'
import ReactDOM from 'react-dom'
import { hello, goodbye } from './lib'

ReactDOM.render(
    <div>
        {hello}
        {goodbye}
    </div>,
    document.getElementById('root')
);

lib.js file lib.js文件

import React from 'react'
import  text from './titles.json'
import './stylesheets/hello.css'
import './stylesheets/goodbye.scss'

export const hello = (
    <h1 id="title"
        className="hello">
        {text.hello}
    </h1>
);
export const goodbye = (
    <h2 id="goodbye"
        className="goodbye">
        {text.bye}
    </h2>
);

titles.json titles.json

{
  "hello" : "Bonjour",
  "bye" : "Au Revoir"
}

i didnot include json loader in webpack.config file as i found out that json loader is added in webpack by default and when i check in browser in console i get this error -> ReferenceError: ReactDOM is not defined. 我没有在webpack.config文件中包含json loader,因为我发现默认情况下在webpack中添加了json loader,当我在控制台中检查浏览器时出现此错误 - > ReferenceError:未定义ReactDOM。

Error that i get in CLI 我在CLI中遇到的错误

当npm开始时cli erro

//Folder Structure //文件夹结构

文件夹结构

The solution seems to be around removing the autoprefixer-loader, I don't know for sure, but it may no longer be necessary to include it because it is included as part of one of the loaders or is built into newer versions of webpack. 解决方案似乎是删除autoprefixer-loader,我不确定,但它可能不再需要包含它,因为它包含在一个加载器的一部分或内置到较新版本的webpack。 Again I'm just speculating here. 我再一次在这里猜测。

The current bang ( ! ) argument separated syntax seems to still work 当前的bang( ! )参数分隔语法似乎仍然有效

{
  test: /\.scss$/,
  loader: 'style-loader!css-loader!sass-loader',
}

But the webpack documentation seems to prefer the broken out style as below 但webpack文档似乎更喜欢下面的分解样式

webpack sass-loader webpack sass-loader

module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['env', 'react'],
        },
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
        ],
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'sass-loader',
          },
        ],
      },
    ],
  },

暂无
暂无

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

相关问题 模块构建失败:TypeError:无法读取未定义的属性“上下文”-Webpack - Module build failed: TypeError: Cannot read property 'context' of undefined - Webpack 模块构建失败:TypeError:无法读取未定义的属性“newLine” - Module build failed: TypeError: Cannot read property 'newLine' of undefined 带有Webpack错误的React Storybook-模块构建失败:TypeError:无法读取未定义的属性&#39;thisCompilation&#39; - React Storybook with Webpack Error - Module build failed: TypeError: Cannot read property 'thisCompilation' of undefined 模块构建失败:TypeError:无法读取未定义的属性'exclude'(awesome-typescript-loader) - Module build failed: TypeError: Cannot read property 'exclude' of undefined(awesome-typescript-loader) 重新安装angular / cli后:模块构建失败:TypeError:无法读取未定义的属性&#39;newLine&#39; - after re-install angular/cli: Module build failed: TypeError: Cannot read property 'newLine' of undefined TypeError:无法在module.exports中设置undefined的属性 - TypeError: Cannot set property of undefined in module.exports 模块构建失败(来自./node_modules/vue-loader/dist/index.js):TypeError:无法读取 Object.loader 未定义的属性“样式” - Module build failed (from ./node_modules/vue-loader/dist/index.js): TypeError: Cannot read property 'styles' of undefined at Object.loader module.exports无法设置未定义的属性 - module.exports Cannot set property of undefined Microsoft NodeJS从零到英雄:找不到模块&#39;../build/Release/bson&#39;和TypeError:无法读取未定义的属性&#39;indexOf&#39; - Microsoft NodeJS Zero to Hero: Cannot find module '../build/Release/bson' and TypeError: Cannot read property 'indexOf' of undefined 调用节点模块失败,并显示以下错误:错误:未捕获(按承诺):TypeError:无法读取未定义的属性“ toLowerCase” - Call to Node module failed with error: Error: Uncaught (in promise): TypeError: Cannot read property 'toLowerCase' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM