[英]How to solve 'Module not found: Error: Can't resolve … multi @babel/polyfill ./react/index.js' in React Webpack Application
We have a React application with webpack and we have an error in our Bitbucket Pipeline when we build the application.我们有一个带有 webpack 的 React 应用程序,当我们构建应用程序时,我们的 Bitbucket Pipeline 中出现错误。
We launch the npm run build
command int the CI inside a Debian docker container and we have this error:我们在 Debian docker 容器内的 CI 中启动npm run build
命令,但出现以下错误:
[INFO] ERROR in ./react/classes/searchparams/parametrosGeneralesForm.js
[INFO] Module not found: Error: Can't resolve '../../dataTables/dataTablesId' in '/opt/atlassian/pipelines/agent/build/bapplication/bapplication-web/src/main/resources/static/resources/react/classes/searchparams'
[INFO] @ ./react/classes/searchparams/parametrosGeneralesForm.js 9:15-55
[INFO] @ ./react/datatables/configurations/configuracion/parametrosGenerales.js
[INFO] @ ./react/datatables/utils/getParameters.js
[INFO] @ ./react/datatables/dataTable.jsx
[INFO] @ ./react/content/body/mainContentBody.jsx
[INFO] @ ./react/content/content.jsx
[INFO] @ ./react/contentWrapper/ContentWrapper.jsx
[INFO] @ ./react/index.js
[INFO] @ multi @babel/polyfill ./react/index.js
We have this error and similars import errors with multiple files.我们有这个错误,并且有多个文件的类似导入错误。
Our .babelrc file is:我们的 .babelrc 文件是:
{
"presets":["@babel/env", "@babel/react"],"plugins": ["@babel/plugin-proposal-class-properties","@babel/transform-async-to-generator","@babel/syntax-dynamic-import"]
}
We have the webpack configuration in two files webpack.base.config.js
and webpack.dev.config.js
.我们在两个文件webpack.base.config.js
和webpack.dev.config.js
有 webpack 配置。
Our webpack.base.config.js
is:我们的webpack.base.config.js
是:
const path = require('path');
const webpack = require("webpack");
const APP_DIR = path.resolve(__dirname, './react');
const PropertiesReader = require('properties-reader');
const Eslanguaje = PropertiesReader('../../languages/bapplication_es.properties')._properties;
const Enlanguaje = PropertiesReader('../../languages/bapplication_en.properties')._properties;
const Defaultlanguaje = PropertiesReader('../../languages/bapplication.properties')._properties;
const ROUTING_PROPS = PropertiesReader('../../routing.properties')._properties;
const CONSTANTS_PROPS = PropertiesReader('../../constants.properties')._properties;
const RemoteProperties = PropertiesReader('../../application-develop.properties')._properties;
const LocalProperties = PropertiesReader('../../application-local.properties')._properties;
const ProductionProperties = PropertiesReader('../../application-production.properties')._properties;
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
let properties="";
switch (process.env.NODE_ENV){
case "local":
properties = LocalProperties;
break;
case "development":
properties = RemoteProperties;
break;
case "production":
properties = ProductionProperties;
break;
}
const springPath = properties["server.servlet.context-path"];
const CONTEXT =(springPath!==undefined&&springPath!==null)?springPath:"";
module.exports = {
entry: {
app: ['@babel/polyfill', APP_DIR + '/index.js']
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.js$/,
use: ["babel-loader", "eslint-loader"],
exclude: /node_modules/
},
{ test: /\.jsx$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
output: {
path: path.join(__dirname, './dist'),
filename: 'bundle.js',
publicPath: CONTEXT+'/res/dist/',
},
externals: {
'esLanguaje': JSON.stringify(Eslanguaje),
'enLanguaje': JSON.stringify(Enlanguaje),
'defaultLanguaje': JSON.stringify(Defaultlanguaje),
'ROUTING_PROPS': JSON.stringify(ROUTING_PROPS),
'CONSTANTS_PROPS': JSON.stringify(CONSTANTS_PROPS),
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.DEBUG': JSON.stringify(true),
'process.env.API_URL': JSON.stringify(CONTEXT)
}),
new HardSourceWebpackPlugin()
]
};
Our webpack.dev.config.js
is:我们的webpack.dev.config.js
是:
const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.config');
module.exports = merge(baseConfig, {
devtool: 'inline-source-map',
mode: "development"
});
The parametrosGeneralesForm.js
file have the correct require to the file path: parametrosGeneralesForm.js
文件对文件路径有正确的要求:
const IDTABLES = require('../../dataTables/dataTablesId');
The file that import is dataTablesId.js
.导入的文件是dataTablesId.js
。
The folder structure is:文件夹结构为:
react
|_dataTables
| |_dataTablesId.js
|
|_classes
|_searchparams
|_parametrosGeneralesForm.js
Is it possible that our webpack.base.config.js
have errors with the resolver code?我们的webpack.base.config.js
是否有可能在解析器代码中出现错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.