2

I've tried to run the function this way:

function helloWorld() {
    console.log("Hello World!");
}

helloWorld()

enter image description here

And I've tried to run the function this way:

function helloWorld() {
    let text = "Hello World!";
    console.log(text);
}

helloWorld()

enter image description here

But either way, nothing is getting logged to the terminal. The first way is giving me a syntax error:

syntax error near unexpected token 'helloWorld'

Can anyone please help me understand why I'm not able to run a simple function in the terminal in Visual Studio Code?

Thank you

EDIT: Added helloWorld() to the bottom of the file.

The file is sitting on my desktop. I tried to enter:

myName-MBP:~ myName$ node </desktop/index.js>

and this returned:

bash: syntax error near unexpected token 'newline'

enter image description here

EDIT 2: The problem was initially solved, but then I started getting this error when running node commands:

internal/modules/cjs/loader.js:582
    throw err;
    ^

Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

As is described in this article. I already had node installed on my system.

And what helped me to fix that issue ^ was to create a new folder on my desktop, place the .js file inside of that folder, open that folder within VS Code, and then type node index.js in the terminal.

5
  • Have you tried clicking over to the "Output" tab? Mar 2, 2019 at 22:10
  • @ScottMarcus yes I can't type anything there - it says "cannot edit in read-only editor" Mar 2, 2019 at 22:11
  • No, I meant after you execute the function from the terminal, switch over to the Output to see if it appears there. Mar 2, 2019 at 22:13
  • Gotcha - no unfortunately there's nothing there after switching over. Mar 2, 2019 at 22:13
  • 1
    According to this, you wouldn't use the terminal to execute code in a separate file. You could enter the function into the terminal and then execute it, just like the browsers Console. Mar 2, 2019 at 22:16

2 Answers 2

3

Do you have nodejs installed? If yes, then you can type node <filepath> where filepath is a path to your file and execute the JavaScript file.

Let's say if your file is called script.js then try typing node script.js to the terminal and see if that helps. Also, make sure your shell is in the current directory.

JavaScript cannot be executed directly in the bash shell. You need nodejs to execute it.

4
  • 1
    Even then nothing would get logged, as helloWorld() would have to be added to the code itself Mar 2, 2019 at 22:20
  • 1
    At the bottom of the file (for example line 19), type helloWorld(). Right now you are only declaring a function. You need to call it too. Otherwise the function will not be executed.
    – arfat
    Mar 2, 2019 at 22:22
  • Okay I've added helloWorld() to the bottom of the file. The file is called index.js and it's sitting on my desktop. Then when I try myName-MBP:~ myNamef$ node </desktop/index.js> I get: bash: syntax error near unexpected token 'newline' Mar 2, 2019 at 22:24
  • 2
    The <...> means that you don't have to type whats in the brackets, but replace it with a value. Do node /desktop/index.js Mar 2, 2019 at 22:26
1

As arfat's answer states, you can run the code with Node.js in the terminal: $ node /desktop/index.js.

Alternatively, you could install the vscode extension Code Runner. This makes it easy to see console logs in the OUTPUT tab with a keyboard shortcut: Ctrl+Alt+N.

2
  • node </desktop/index.js> is still giving me bash: syntax error near unexpected token 'newline' Mar 2, 2019 at 22:47
  • 1
    As Jonas Wilms stated, remove the angle brackets: node /desktop/index.js Mar 2, 2019 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.