I want to use the node.js on the windows. so I install node but I can't execute a .js file. That is my source code. And I atteched the error screen.
helloworld.js
console.log("Hello, World!")
This error is happening because your script is not executed with nodejs but with Microsoft jscript.
The command you wrote "node node.js" is executing the file ode.js instead of the nodejs in your system in the same way you can execute cmd by typing cmd instead of cmd.exe.
To fix that you need to do one of those :
rename the file to something other than node
move the file named node in a subfolder (i.e. src/node.js)
associate .js file with nodejs instead of microsoft jscript
Related
I want to execute JavaScript code in the Visual Studio Code terminal. I installed the Code Runner extension, but it is executing code in "Output" where I am not able to enter run-time input values.
To execute in "terminal", I have to type "node pgm.js". Is there a way by which I can directly execute JavaScript files in the Visual Studio Code terminal by using shortcut Ctrl + Alt + N?
Go to settings and configure the Code Runner extension to output to the terminal.
Also, you need Node.js for it. If that doesn't work, you don't need code runner. Use Node.js from a normal terminal.
From the top, go to menu View → Terminal to open the normal terminal. Then run the command node filename.js to execute it.
I opened the terminal on my mac, and first checked that I have node installed: node -v
v14.17.5
I then tried to open a file I created index.html from Visual Studio Code and I got a throw err from the terminal. { The code:'Module_NOT_FOUND', requireStack:[] }
Thank you in advance for help.
node is a runtime environment that can execute javascript on a machine. So it can't read html file, but only js files. You can execute a js file with the following command in the terminal: node index.js
Et voilà.
Please note you can also write javascript directly in your terminal by entering a node environment.
node
const a = "hello"
a
// return "hello"
I think you are trying to run an html file using node
just change the index.html to index.js
I need to read CSV file, process the file via REST API call and then write the output in another CSV file. The requirement is strictly shell script using NodeJs.
I am using Windows platform
You could add a shebang to your .js file and make it executable.
#!/usr/bin/env node
console.log('...')
Alternatively you make a wrapper bash script and execute whatever you want
#!/usr/bin/env bash
node whatever.js
If you are on linux, start your script with the following line:
#!/usr/bin/env node
This would indicate that node interpreter should be used to execute this file.
Then make the file executable:
chmod +x your_script.js
At this point it should be possible to run script as any regular executable, e.g:
./your_script.js
i m trying to create local enviroment for learning node.js
i m following the link below
http://www.tutorialspoint.com/nodejs/nodejs_environment_setup.htm
i downloaded Windows Installer (.msi) on my computer and create a file named main.js
console.log("main.js");
and i double clicked node.exe
on command prompt i m getting unexpected identifier exeption what s wrong?
After answers i also tried followings
i created a new folder on my desktop and accessed to it. i got same error.
i also used Windows Powershell
Don't put your files into the nodejs-folder!
Create a new folder (e.g. on your Desktop) and put main.js in it
Start a command prompt (hit Windows+R and type cmd.exe)
Navigate to your newly created folder using cd
Run main.js by typing node main.js
If you enter node and press enter, you start something like a console. You can directly write JavaScript commands, e.g.
C:\Temp>node
> console.log('Hello World');
Hello World
undefined
>
If you started node and then enter node main.js, node will try to understand this command as JavaScript which is obviously not JavaScript. What you want to do is to enter node main.js directly:
C:\Temp>node main.js
Hello World
See the difference: In the first example you start node and then enter some JavaScript commands and in the second example you start node but with the parameter main.js which tells node not to start this "console" but to load this file and run it.
You are expected to type node filename.js from your command line shell.
You've run node (presumably by double clicking its icon) and are trying to type node filename.js from the node REPL instead of your shell.
Open Zsh, Bash, Windows Powershell or similar and run it from there.
Access your file from a command Prompt like node yourFile. Also make sure you have installed node properly.To check that just type node -v, it should give you the current node version. If all these things are done then check for errors in server.js, which is main.js in your case.
I am try loading a simple js file on the node.js console and it never shows me the result. I have a test.js file with a single line: console.log("testing");
If I enter directly the command into the console it works but by doing node test.js it shows me ... loading.
You are trying to invoke the command node test.js when already inside the REPL (the node.js console as you put it). The REPL only accepts JavaScript.
The command node test.js within the REPL is therefore a JavaScript syntax error, however the REPL attempts to recover from syntax errors by buffering the command and prompting you for more input (i.e. ...). See here in the REPL code.
You can either
Exit the REPL and from your terminal/console provide the test file as an argument to the node executable, i.e. node test.js (as I think you were intending to do)
Or as #JonathanLonowski suggests, within the REPL you can use:
require('./test')
child_process.fork('./test')
(When starting the node REPL in the directory where the test file is)