9

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.

jo_va
  • 13,504
  • 3
  • 23
  • 47

6 Answers6

23

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)

dmcquiggin
  • 3,230
  • 24
  • 37
9

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

xiaomuzizier
  • 91
  • 1
  • 1
1

The readme for the plugin says that you should add an "executorMap".

Open the user settings (on Mac Cmd + Comma, on Windows Shift+Alt+Comma) and add the following to the JSON:

{
    "code-runner.executorMap": {
        "javascript": "node"
    }
}
1

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

fusion27
  • 2,396
  • 1
  • 25
  • 25
1

If you have brew installed, then just run this on Terminal, brew install node

It will install node. Now run, which node

and you will see some thing like this, /usr/local/bin/node

Now run your program and you should be good to go.

Amit Gupta
  • 11
  • 2
0

That program looks like running a node for js. Why not just using the built in terminal in the visual studio code with nodejs?
You just need to install the nodejs: https://nodejs.org/en/
Then in visual studio code press ctrl + ` On the terminal you have to say type:
node myapp.js
Then on the terminal it prints out your data.
(that solution is more 'professional like')
Welcome in the world of JavaScript!

aboutus
  • 229
  • 4
  • 13
  • 1
    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 –  Jul 08 '17 at 07:52
  • 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) – aboutus Jul 08 '17 at 08:51
  • 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. –  Jul 08 '17 at 09:03
  • 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. –  Jul 08 '17 at 09:06
  • 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 Jul 08 '17 at 10:07
  • (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) – aboutus Jul 08 '17 at 10:12