[英]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中遇到的错误
//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.