无法在反应中解析模块
Can't resolve module in react
根据以下内容:Webpack 2:
我尝试使用 modules
而不是 alias
来加载我的组件,如下所示:
resolve: {
modules: [__dirname, 'node_modules',path.resolve('./app/components')],
alias:{
applicationStyles:'app/styles/app.scss'
},
但我收到以下 webpack 错误:
ERROR in ./app/app.jsx Module not found: Error: Can't resolve
'TodoApp' in 'C:....' @ ./app/app.jsx 10:14-32 @ multi
babel-polyfill script-loader!jquery/dist/jquery.min.js
script-loader!foundation-sites/dist/js/foundation.min.js ./app/app.jsx
一切顺利,如果我对每个组件使用 alias
而不是这样:
resolve: {
modules: [__dirname, 'node_modules'],
alias:{
applicationStyles:'app/styles/app.scss',
TodoApp: 'app/components/TodoApp.jsx'
},
extensions: ['*','.js','.jsx;']
},
您在 resolve.extensions
属性 中有 semicolon
:'.jsx;'
。
这个分号是 resolve.modules
不起作用的问题的根源。
当您删除此分号并将 'app/components'
添加到 resolve.modules
时,webpack 打包时没有错误。
webpack.config.js (修改后)
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: [
'babel-polyfill',
'script-loader!jquery/dist/jquery.min.js',
'script-loader!foundation-sites/dist/js/foundation.min.js',
'./app/app.jsx',
],
output: {
path: __dirname,
filename: './public/bundle.js',
},
externals: {
jquery: 'jQuery',
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
],
resolve: {
modules: [__dirname, 'app/components', 'node_modules'],
alias: {
applicationStyles: 'app/styles/app.scss',
},
extensions: ['*', '.js', '.jsx'],
},
module: {
rules: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'es2017', 'stage-0'],
plugins: ['transform-runtime'],
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
},
{
test: /\.scss$/,
use: [
{
loader: 'sass-loader',
options: {
includePaths: ['./node_modules/foundation-sites/scss'],
},
},
],
},
],
},
devtool: 'cheap-module-eval-source-map',
};
根据以下内容:Webpack 2:
我尝试使用 modules
而不是 alias
来加载我的组件,如下所示:
resolve: {
modules: [__dirname, 'node_modules',path.resolve('./app/components')],
alias:{
applicationStyles:'app/styles/app.scss'
},
但我收到以下 webpack 错误:
ERROR in ./app/app.jsx Module not found: Error: Can't resolve 'TodoApp' in 'C:....' @ ./app/app.jsx 10:14-32 @ multi babel-polyfill script-loader!jquery/dist/jquery.min.js script-loader!foundation-sites/dist/js/foundation.min.js ./app/app.jsx
一切顺利,如果我对每个组件使用 alias
而不是这样:
resolve: {
modules: [__dirname, 'node_modules'],
alias:{
applicationStyles:'app/styles/app.scss',
TodoApp: 'app/components/TodoApp.jsx'
},
extensions: ['*','.js','.jsx;']
},
您在 resolve.extensions
属性 中有 semicolon
:'.jsx;'
。
这个分号是 resolve.modules
不起作用的问题的根源。
当您删除此分号并将 'app/components'
添加到 resolve.modules
时,webpack 打包时没有错误。
webpack.config.js (修改后)
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: [
'babel-polyfill',
'script-loader!jquery/dist/jquery.min.js',
'script-loader!foundation-sites/dist/js/foundation.min.js',
'./app/app.jsx',
],
output: {
path: __dirname,
filename: './public/bundle.js',
},
externals: {
jquery: 'jQuery',
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
],
resolve: {
modules: [__dirname, 'app/components', 'node_modules'],
alias: {
applicationStyles: 'app/styles/app.scss',
},
extensions: ['*', '.js', '.jsx'],
},
module: {
rules: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'es2017', 'stage-0'],
plugins: ['transform-runtime'],
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
},
{
test: /\.scss$/,
use: [
{
loader: 'sass-loader',
options: {
includePaths: ['./node_modules/foundation-sites/scss'],
},
},
],
},
],
},
devtool: 'cheap-module-eval-source-map',
};