How to run javascript code in Visual studio code? /bin/sh: 1: node: not found

15,717

Solution 1

I had the same issue with this (very useful) extension, but the solution is straight forward.

  1. Locate the path to your Node executable, by typing the following command in a terminal:

which node

The result will be similar to the following (I use nvm to manage my Node versions, yours might look a little different)

/home/my_username/.nvm/versions/node/v10.15.1/bin/node

Make a note of / copy this path.

  1. Open VS Code. Either press Ctrl+, (on Linux), or from the File menu, select Preferences > Settings.

In the search box at the top of this window, type:

Executor Map

Click the 'Edit in settings.json' link displayed under the first result.

Add the following to the end of the settings file, replacing the path with the one from step 1.

"code-runner.executorMap": {
            "javascript": "/home/my_username/.nvm/versions/node/v10.15.1/bin/node"
}

The Extension should now work as planned (tested on Ubuntu 18.04)

Solution 2

Please use below setting (File->Preference->Settings) to run code in Integrated Terminal:

{
"code-runner.runInTerminal": true

}

the answer from: https://github.com/formulahendry/vscode-code-runner/issues/355

Solution 3

In Ubuntu 18.04

Turns out a NodeJS install was required and Code Runner worked like champ. The which node command exposed the issue

BEFORE

which node
node not found

NODE + NPM INSTALLATION

sudo apt update && sudo apt install nodejs -y && sudo apt install npm -y

AFTER

which node
/usr/bin/node

👍️ CODE RUNNER WORKS

Share:
15,717
jo_va
Author by

jo_va

Enthusiastic full-stack web developer and ex-mechanical engineer. I work mainly with JavaScript/TypeScript, NodeJS and React but I have experience with other technologies as well, such as Angular, React Native and even embedded software development. Forever lover of tech & dinosaurs ❤️

Updated on June 08, 2022

Comments

  • jo_va
    jo_va almost 2 years

    I just started learning programming and I installed Visual Studio Code to write javascript in.

    I installed the Code Runner extension, to help me run my code.

    Whenever I try to run my code it says:

    /bin/sh: 1: node: not found

    and nothing happens.

    How do I fix this? I am trying to make hello world appear, but it just says node not found.

    • NicoXiang
      NicoXiang almost 7 years
      it works fine in my enviroment. is it just simle js code or typescript? try to install nodejs first.
  • Admin
    Admin almost 7 years
    actualy it doesnt work. I just tried making a new program but it keeps saying the same thing when I run it. It just says this when I try it your way - node myapp.js module.js:328 throw err; ^ Error: Cannot find module '/home/david/webdev/myapp.js' at Function.Module._resolveFilename (module.js:326:15) at Function.Module._load (module.js:277:25) at Function.Module.runMain (module.js:442:10) at startup (node.js:136:18) at node.js:966:3
  • aboutus
    aboutus almost 7 years
    Hmm, it looks like the node just doesn't find your app. Maybe you can try node ./myapp.js With the ./ you explicit says thats program is in that current directory, where your are in the terminal. (optionally you can leave the .js from the command, like node myapp, because nodejs will know, you want to run a javascript file)
  • Admin
    Admin almost 7 years
    I was confused when you said type node myapp.js. I literally typed node myapp.js, instead of the name of my file which is hiuhiu.js. When I tried node hiuhiu.js, it worked for the first time I tried it and it printed the number 42. But I changed the code in the same file to say hello world now and when I type node hiuhiu.js it says "node hiuhiu.js SyntaxError: Unexpected identifier" and a bunch of other random stuff I don't understand. All I had in my code was console.log("hello"); so I don't understand the error.
  • Admin
    Admin almost 7 years
    I just tryied using node ./hiuhiu.js and node hiuhiu but its just showing me ... I think I'm going to give up on making it work in visual code studios. Is there a better way to run javascript code, or a standard way people use? I was using an editor in my firefox browser earlier and it worked fine no issues, but it was hard to see my code so I downloaded visual code studios. Now my code doesn't work there.
  • aboutus
    aboutus almost 7 years
    Never give up :) It looks like the node is working fine as the first time runned your code, the second time there should be and error in your code. The quotes at good position etc? Btw, if you don't want to work in browser, that's the standard way. But you don't need the visual studio code for this. As i see you are on linux, so just start a linux terminal, navigate to your folder, then run your .js file with the node. There is no tricky setup, just need an installed nodejs, a js file, and the terminal.
  • aboutus
    aboutus almost 7 years
    (alternatively on your browser at any page you can press ctrl + shift + i, it's brings up a js console, and there you can test small scripts, but that's good for small tests)