A note about writing Node.js application with Webpack

First, you may ask why would we use Webpack for writing Node.js application? Long time ago, we had to use Webpack with Babel to get some ES6 features working, today, most of the features are supported, but we still want to write our application in TypeScript, hence, webpack and ts-loader.

By default, Webpack generates a bundle that inside a browser, with a target: 'web' in its config, in this mode, Webpack also override some Node.js variables, like process:

process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = '';
process.versions = {}

It also set process.argv = [], which is a widely used variable in Node.js to get command line arguments. So, the following command will always return an empty array:

// output: []

We should set the target to another value, to make sure everything in Node.js environment will work as expected:

target: 'node'

Date: 2019-01-23 Wed 00:00

Author: Huy Tran

Created: 2019-04-22 Mon 14:41