Follow @endyourif rss Facebook LinkedIn

Uncaught ReferenceError: require is not defined Uncaught ReferenceError: require is not defined

Published on Feb 23, 2020

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();.

Tags: Node js Tutorial | JavaScript | express

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP