Skip to Content
Course content

142: Running JavaScript Outside the Browser

Click on the "Edit" button in the top corner of the screen to edit your slide content.

How do I actually get a .js file to run on my machine without opening a browser?

Up until now, you've probably been relying on the browser to be the "engine" that executes your code. But to run JavaScript standalone, you need a runtime environment installed on your OS. The industry standard for this is Node.js. Think of Node as taking the V8 engine—the same thing that powers Chrome—and wrapping it in a way that it can run directly on your hardware.

Once you've installed Node, you don't need an HTML file to act as a bridge. You just create a plain .js file and call it from your terminal. For example, if you have a file called process-users.js that filters a list of users, you just run:

node process-users.js

I'll show you a quick example. Instead of a generic print statement, let's imagine you're writing a script to clean up a list of messy usernames:

const users = ["  alice_99  ", "BOB_smith", "  Charlie_Brown  "];
const cleanedUsers = users.map(u => u.trim().toLowerCase());

console.log("Cleaned User List:", cleanedUsers);

Run that with Node, and the output goes straight to your terminal. No DevTools, no console tab—just raw output in your command line.

Why am I getting "ReferenceError: document is not defined" now?

This is the first wall every developer hits when moving to the server side. You have to realize that JavaScript the language is different from the environment it lives in. The document, window, and localStorage objects aren't actually part of JavaScript; they are Web APIs provided by the browser to let you manipulate the DOM.

Node.js doesn't have a DOM because there is no visual window to manipulate. If you try to use document.querySelector() in Node, it'll crash because that object literally doesn't exist in this environment. Instead, Node gives you a different set of tools. For instance, instead of window, you have global. Instead of browser alerts, you have process.stdout.write() or simply console.log().

Can I actually interact with my computer's files using JavaScript?

Yes, and this is where it gets powerful. While the browser keeps JavaScript in a "sandbox" for security (you wouldn't want a random website deleting your photos), Node.js has full access to your system. To do this, you use built-in modules. The most common one for this is the fs (File System) module.

I use this all the time for writing quick automation scripts. Here is how you'd write a simple script to create a log file on your hard drive:

const fs = require('fs');

const logMessage = `Log entry created at: ${new Date().toLocaleString()}\n`;

// This creates a file called 'app.log' (or appends to it if it exists)
fs.appendFileSync('app.log', logMessage);

console.log("Successfully wrote to the log file!");

Just be careful. Since you're no longer in a sandbox, a fs.unlinkSync() call can permanently delete a file. You have the keys to the kingdom now, so use them wisely.




📋 Practical Task

Build a Local Project Manifest Generator

Your task is to create a Node.js script that generates a "manifest" file for a project. Instead of manually typing out a project description, you'll write a script that saves this data to a file.

  • Create a file named manifest-gen.js.
  • Define an object called projectDetails containing a projectName, version, and author.
  • Use the fs module to convert that object into a string (hint: use JSON.stringify() with formatting) and write it to a file named manifest.json.
  • Run the script using the terminal command node manifest-gen.js and verify that the manifest.json file was created in your folder with the correct content.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.