Solve the error problem of browser require is not defined

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 requireit is CommonJs in the node environment, importbut ESModule in the browser environment. ESModule does not recognize require.
requireis 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.jsthe original to .target: 'node'target: 'web'
webpack.config.js

hot knowledge

export / export default + import ===》 ES6
module.exports / exports + require ===》CommonJS

Guess you like

Origin blog.csdn.net/weixin_42280517/article/details/127241185