Uncaught ReferenceError: require is not defined

U

When you’re first starting out with Node js, it might be a bit confusing because you are most likely accustomed to writing JavaScript code for the web browser. When Node js is the server, the syntax is similar but there are a few differences around how other JavaScript files and modules are included. When writing on the server side you may include files as follows: require(‘./mysharedfile’);.

This however is not valid on the client-side. I’ve previously written an article about Include a JavaScript file in another file.

Another good thing to remember is that when you are writing your Node js server-side code you split it into multiple files; ideally one per module keeping the single responsibility principle in mind. This is also good practice when you are doing client-side development as well.

Don’t be afraid about making a lot of JavaScript files, just be sure that you implement a JavaScript minify so that all of your JavaScript is compiled into a single file and it actually removes the entire need to even used the require();.

About the author

By Jamie

My Books