-1

I'm trying to run this script in vscode :-

#!"C:\Program Files\nodejs\node.exe"

console.log("Hello world");

And in command line I enter :-

./abc.js

Each time I run this, the cursor goes to the end of script. Whereas, I'm trying to see "Hello World" on command line. Is something wrong with the shebang line.

Also, Ques.2 Is it possible to run the script without the filename also. For e.g. in the following code :-

#!"C:\Program Files\nodejs\node.exe"

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

In command Line I would simply enter this :-

hello
Tanish
  • 35
  • 4

2 Answers2

1

You can embed this information into your JavaScript file with a "shebang" line. The "shebang" is the first line in the file, and tells the OS which interpreter to use for running the script. Below is the first line of JavaScript:

linux = #!/usr/bin/node windows = #!node

  • Will node.js ignore this line? That looks like it would cause an error. – RamenChef Oct 19 '22 at 02:38
  • your node js file look something like this in windows ================================== #!node console.log("working"); ================================== Shebang or hashbang (#!) is the first line of the file which tells the OS which interpreter to use. – Isfhan Ahmed Oct 20 '22 at 07:07
0
  1. npm init your project. this will create package.json file in your working project folder.

  2. Open package.json file, under scripts section write a script: Mine looks like this:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "local": "nodemon server.js"
  }

when I run npm local in command line, it will run server.js file. P.S. nodemon is a node.js framework. either you need to install it (npm install nodemon -g) or use node server.js command instead.

jkalandarov
  • 564
  • 5
  • 15