npm ERR! Code elifecycle solution
1. Problems
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build:webpack --config config/webpack. config. js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.…
The reason for this is the webpack.config.js configuration error, which is actually a path configuration error. Some path configurations in webpack.config.js are configured according to the package.json file location, and some are configured according to the current file location, and people tend to get confused when configuring them.
2. Solutions
2.1 Solution 1
Fill in the correct path, this problem is often caused by the path configuration error. Refer to the following template, I will decipher the path configuration.
entry:’. /src/js/index.js’ // this path is based on the package.json file as the base file path (not the webpack.config.js file)
template in plugins: ‘src/index.html’ //this is also based on the package.json file as the base file path
and path:path.resolve(__dirname,’…/dist’) // is based on the webpack.config.js file path
const path=require(‘path’); // call the path in node.js
const HtmlWebpackPlugin = require(‘html-webpack-plugin’);
module.exports={undefined
entry:’. /src/js/index.js’, // the file to be packaged (this path is written according to the package.json location)
output:{undefined
filename:’bundle.js’, //the name of the package file
path:path.resolve(__dirname,’…/dist’) //specify the generated file directory (written by the current file location)
},
module: {undefined
rules: [
{undefined
test: /.css$/,
use: [ ‘style-loader’, ‘css-loader’ ]
}
]
},
plugins: [
new HtmlWebpackPlugin({undefined
template: ‘src/index.html’ //configure html template (by package.json location)
})
]
}
2.2 Solution 2
Direct reinstallation, but personally do not recommend, indeed some files reinstallation is possible to solve the problem, but sometimes reinstallation is very slow, which is annoying. Reinstallation steps are as follows.
(Do not delete package.json, delete package-lock.json)
npm cache clean --force
rm -rf node_modules
rm -rf package-lock.json
npm install