VSCode中运行JavaScript代码提示/bin/sh: node: command not found问题求助
Hey there, let's get that annoying error sorted out—this is a super common hiccup when setting up JavaScript in VS Code, so we'll walk through it step by step.
First: Make sure Node.js is actually installed
The error means VS Code can't find the node command, which usually starts with not having Node.js installed at all. Let's check:
- Open your system's terminal (not the one inside VS Code)
- Run these commands:
node -v npm -v - If you don't see version numbers pop up, head over to the Node.js official site and install the LTS version, following the default setup steps.
If Node is installed: Point Code Runner to the correct Node path
Sometimes VS Code can't automatically find where Node is installed, so we need to tell it explicitly:
- Find your Node executable path:
- Windows: Open Command Prompt and run
where node—it'll give you a path likeC:\Program Files\nodejs\node.exe - Mac/Linux: Open your system terminal and run
which node—you'll get something like/usr/local/bin/node
- Windows: Open Command Prompt and run
- Open VS Code settings: Press
Ctrl+,(Windows/Linux) orCmd+,(Mac) - Search for Code Runner: Executor Map, then click "Edit in settings.json"
- In the settings.json file, find the line for
"javascript"—it probably looks like this:"javascript": "node", - Replace
"node"with the full path you found earlier. Remember to escape backslashes on Windows:- Windows example:
"javascript": "C:\\Program Files\\nodejs\\node.exe", - Mac/Linux example:
"javascript": "/usr/local/bin/node",
- Windows example:
- Save the file, then restart VS Code to apply the changes.
Quick troubleshooting tip
If you just installed Node.js right before opening VS Code, close VS Code completely and reopen it—sometimes the editor doesn't pick up new environment variables until it's restarted.
A tiny note on your test code
Your code is totally valid, but heads up: alert() is a browser-only API. If you run this in the terminal via Code Runner, you'll get a new error because Node.js doesn't have alert() built-in. Swap it out for console.log(admin) instead, and you'll see "John" print directly in the terminal—perfect for testing!
内容的提问来源于stack exchange,提问作者Carambaman

