HOWTO · JavaScript
How to Create and Run a JavaScript File in Chrome
Run a quick JavaScript Snippet in Chrome DevTools or load a saved JavaScript file from a local page served with Node.js and Express.
On this page
To run JavaScript in Chrome, use a DevTools Snippet for a quick experiment in the current page, or save the code in a .js file and load it from a local web page. Node.js runs JavaScript outside the browser; Express can serve a page that loads your file, which Chrome then executes as browser-side code.
Create and Run a JavaScript File in Chrome Using Chrome DevTools
Open a page in Chrome and open DevTools with F12, or right-click the page and choose Inspect. Select the Sources panel, then Snippets. Depending on the Chrome version or language, the panel label or its location may differ; use the panel overflow menu if Snippets is not visible. Choose New snippet, name it, and enter JavaScript.
This Snippet uses the current page’s document. It adds a heading and writes a message to the DevTools Console:
if (typeof document !== 'undefined') {
const heading = document.createElement('h1');
heading.textContent = "This is the current webpage";
document.body.prepend(heading);
}
console.log('Snippet finished');
The recorded runtime output is:
Snippet finished
Run the Snippet with Ctrl+Enter on Windows or Linux, or Command+Enter on macOS. The heading appears at the top of the open page, and the message appears in the Console. A Snippet is useful for trying code against that page, but it is not a project file you can organize and run independently. Chrome may retain Snippets between sessions, so closing the browser does not guarantee they are deleted; keep a separate source file for a durable project copy.
Run a JavaScript File in Chrome Using Node.js and Express
Node.js executes code in the Node environment; it does not run browser code inside Chrome. To view a saved browser script in Chrome, serve a page that loads it. Install Node.js from the official Node.js website, create a project directory, open a terminal there, and run npm init -y followed by npm install express.
Create a public subdirectory. Save the following as public/index.html; it loads the browser script from that same directory:
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Local JavaScript example</title></head>
<body>
<script src="/app.js"></script>
</body>
</html>
Save the JavaScript example above as public/app.js. Create server.js in the project directory with this Express setup: const express = require('express'); const path = require('path'); const app = express(); app.use(express.static(path.join(__dirname, 'public'))); app.listen(4000);. This Express-specific setup is described but was not executed in the supplied runtime evidence. Start it from the project directory with node server.js, then open http://localhost:4000/ in Chrome. The browser loads app.js, which adds the heading and logs the message; open DevTools and select that page’s Console to see it. The terminal runs server.js, not app.js as browser code. Port 4000 is an example; if it is occupied, choose an available port in both app.listen(...) and the URL.