How does require() in node.js work?

Source code is here. exports/require are not keywords, but global variables. Your main script is wrapped before start in a function which has all the globals like require, process etc in its context. Note that while module.js itself is using require(), that’s a different require function, and it is defined in the file called “node.js” … Read more

Conditional build based on environment using Webpack

You can use the define plugin. I use it by doing something as simple as this in your webpack build file where env is the path to a file that exports an object of settings: // Webpack build config plugins: [ new webpack.DefinePlugin({ ENV: require(path.join(__dirname, ‘./path-to-env-files/’, env)) }) ] // Settings file located at `path-to-env-files/dev.js` … Read more

Is ‘require(…)’ a common javascript pattern or a library function?

The require() idiom is part of a specification known as CommonJS. Specifically, that part of the spec is called ‘Modules’. RequireJS is just one implementation of CommonJS (and it’s usually a browser-side implementation – in fact, it takes a different approach because of the asynchronous nature of the browser). If you look at the list … Read more