Tag Archives: browserify

JS error: require is not defined

Reasons for error:
The browser side cannot recognize the require keyword, which is required under node.js environment. It is common to require modules in node_modules folder
Solutions:
Compile the JS file with the tools Browserify or WebPack to make it browser-aware.
// Install Browserify, which I’m using as a global installation
npm install -g browserify
//compile
browserify ./source/module.js -o ./dist/dist.js
You can see the packaged Dist. Js file in the dist directory. The first parameter after
browserify represents the entry of the front-end program to be packaged, -o or > Represents the packaged output file. Browserify automatically completes dependency analysis based on require or Import (ES6, need to install Babel) in the entry file and packages the dependency file into a single file.