reason
Some packages are introduced into the node environment, packaged with webpack and placed in the browser environment, and the browser reports an error require is not defined
.
This is because require
it is CommonJs in the node environment, import
but ESModule in the browser environment. ESModule does not recognize require.
require
is a private global method of node.
Solution
Then let the packaged code not have require. In other words, webpack should package a code suitable for the ES6 environment.
Just change webpack.config.js
the original to .target: 'node'
target: 'web'
hot knowledge
export / export default + import ===》 ES6
module.exports / exports + require ===》CommonJS