webpack Usage of Webpack Usage CommonJS modules example


Example

Create folder. Open it in command line. Run npm install webpack -g. Create 2 files:

cats.js:

var cats = ['dave', 'henry', 'martha'];
module.exports = cats;

app.js

cats = require('./cats.js');
console.log(cats);

Run in command line: webpack ./app.js app.bundle.js

Now in folder will be file app.bundle.js. You can include it in index.html page, open it in browser and see result in console.

But more fast way is run in command line: node app.bundle.js and see result immediately in console:

[ 'dave', 'henry', 'martha' ]