How to open .JS files on Mac
To open .JS files on Mac, to view or edit: Control-click → Open With → VS Code or another editor. Prefer that over double-click until you know how .js is associated.
Step-by-step instructions
- To view or edit: Control-click → Open With → VS Code or another editor. Prefer that over double-click until you know how .js is associated.
- To run with Node: Install Node if needed, open Terminal, cd to the directory, run: node ./yourfile.js. If the file starts with #!/usr/bin/env node and is executable (chmod +x), you can run ./yourfile.js from the same directory.
- For web front-end projects, .js files are usually bundled or served by a dev server (Vite, webpack, etc.); follow the project README instead of guessing a generic “compatible app.”
Common issues
Double-clicking runs or flashes a console instead of opening the editor
File associations may point to wscript/cscript, node, or another host, so Explorer treats the file as executable.
- Right-click → Open with → choose your editor, then use “Always” / Default apps to keep .js opening for editing.
- Run scripts only from a terminal with an explicit command: node yourfile.js.
'node' is not recognized (Windows) or command not found (macOS/Linux)
Node.js is not installed or not on your PATH.
- Install Node.js LTS and reopen the terminal, or on Windows use the installer option to add to PATH.
- Confirm with node -v; if multiple versions exist, use a version manager (nvm, fnm, volta) as your team recommends.
Error about "Cannot use import statement outside a module" or require()
Node treats .js as CommonJS by default unless package.json sets "type": "module" or you use .mjs; bundlers and browsers have their own rules.
- Read the project’s package.json and README for module mode.
- Rename to .cjs/.mjs only when you understand the implications; renaming alone does not fix logic errors.
The file looks fine in an editor but the browser still blocks it
Web servers must send text/javascript (RFC 9239); HTML <script type="module"> vs classic scripts also changes behavior.
- Verify response headers and <script> attributes in DevTools.
- Consult MDN or the HTML Standard for script loading and MIME requirements.
Security note
JavaScript can perform network requests, file access (within Node permissions), and process control. Never run unfamiliar .js with node or npm npx without reading the source and understanding what install scripts execute.