Posted By: Anonymous
I am writing an application with the Node.js, Express.js, and Jade combination.
I have file
var m = require('./messages');
in order to load the contents of
messages.js (just like I do on the server side) and later on call functions from that file. However,
require is not defined on the client side, and it throws an error of the form
Uncaught ReferenceError: require is not defined.
messages.js) in the main
client.js file that opens the socket to the server?
This is because
You have three options:
- Use the
- Use a CommonJS implementation. It has synchronous dependencies like Node.js
- Use an asynchronous module definition (AMD) implementation.
CommonJS client side-implementations include (most of them require a build step before you deploy):
- Browserify – You can use most Node.js modules in the browser. This is my personal favorite.
- Rollup – a new contender. It leverages ES6 modules and includes tree-shaking abilities (removes unused code).
You can read more about my comparison of Browserify vs (deprecated) Component.
AMD implementations include:
Note, in your search for choosing which one to go with, you’ll read about Bower. Bower is only for package dependencies and is unopinionated on module definitions like CommonJS and AMD.