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

6

10

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.

Steck answered 8/7, 2017 at 7:1 Comment(1)
it works fine in my enviroment. is it just simle js code or typescript? try to install nodejs first. – Gamelan
D
25

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)

Doublespace answered 13/2, 2019 at 20:20 Comment(0)
B
11

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

Brieta answered 29/2, 2020 at 10:31 Comment(0)
G
2

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.

Godevil answered 9/2, 2023 at 8:3 Comment(0)
K
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"
    }
}
Kirven answered 13/3, 2018 at 12:12 Comment(0)
J
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

Josi answered 18/4, 2020 at 15:44 Comment(0)
R
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!

Rustle answered 8/7, 2017 at 7:11 Comment(6)
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 – Steck
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) – Rustle
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. – Steck
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. – Steck
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. – Rustle
(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) – Rustle

© 2022 - 2024 β€” McMap. All rights reserved.