JavaScript(Page 2)

How to fix “__dirname is not defined in ES module scope”

⤳ The error “__dirname is not defined in ES module scope” occurs if you refer to the __dirname global variable in an ES (ECMAScript) module. Here’s what the error message looks like: This global variable contains the path to the current module’s directory. The __dirname and ...

⌛ 2 min read

JavaScript isset Equivalent (3 methods)

⤳ If you’re looking for the JavaScript isset equivalent, you’ve probably used it in PHP before and want the same functionality in JavaScript. As you probably know, PHP’s isset() function checks a variable is declared and not null. Although JavaScript doesn’t have an isset equivalent, there are ...

⌛ 6 min read

Integer division in JavaScript explained

⤳ In this guide, you’ll learn how to do integer division in JavaScript. The division operator in JavaScript (/) divides two numbers (dividend and divisor) and returns the quotient as a floating point number (rather than the quotient and the remainder of a division separately). All numbers in ...

⌛ 4 min read

Cannot find module error in Node.js (Fixed)

⤳ “Error: cannot find module” occurs when you try to load a non-existant module in Node – Either via ESM (ECMAScript Modules) or CommonJS module systems.  The error usually looks like this in the console: Why does “Error: cannot find module” occur? The error “cannot find ...

⌛ 8 min read

Add commas to numbers in JavaScript with these simple methods

⤳ This guide explores the three common ways to add commas to numbers in JavaScript. Displaying numbers – whether currency or plain numbers – in an easy-to-read format improves your HTML page content and overall user experience. We usually achieve this by using commas as thousands ...

⌛ 5 min read

SyntaxError: Unexpected end of JSON input in JavaScript (Fixed)

⤳ You might have encountered the error “Uncaught SyntaxError: Unexpected end of JSON input” when using JSON.parse(). The error looks like this on the browser console (Google Chrome): First of all, it’s not your code! What’s most likely wrong is the JSON string you’re trying to parse. ...

⌛ 3 min read