Not able to run node in the terminal - javascript

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

Related

My VS CODE extension code runner isnt working

Hi my issue is that in my visual studio code, my code runner extension doesn't provide any output. I've looked at a bunch of tutorials and have done everything, research, download node.js,and saves the file as a javascript file and verify paths.
I entered " console.log("Hello")
Its prints this out
[Running] node "c:\Users\NAME\Untitled-1.js
[Done] exited with code=0 in 0.202 seconds
And nothing
Please help idk what I'm doing wrong 😭
Looks like you haven't saved the file.
Try saving the file and then run with Code Runner.
Tip: You can enable Save before Run settings so that it gets saved every time before running the file.
File -> Preferences -> Extensions -> Run Code Configuration -> Save File Before Run
I recommend you to run node -v and npm -v in command prompt window to see if you have them installed
the output must be like this
C:\Users\Anonymous>node -v
v16.14.0
C:\Users\Anonymous>npm -v
8.3.1
and if there is a problem it might be from your paths ("Edit system environment variables" in windows search) or you haven't installed npm or node.
in my case solution was to uncheck "Show Execution Message" check box in Run Code configuration.
setting
result

How do you change the directory of the terminal in a Node JS script

Let's say I do node someCommand.js. I want this command to change the directory of the terminal.
When I tried process.chdir(newDir), that just changes the directory in the context of the script - once the command exits, the terminal is still in the old directory.

Node js 'console' error in cmd

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

node js local enviroment setup

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.

Node.js execute Javascript file

This is a completely newbie question and I'm sure the solution is very easy but I don't get it.
I downloaded Node.js and put the command-tool file and the npm in my project file.
I created a file called example.js which contains
console.log("hello world");
Now I open the node file and type in
node example.js
And it shows me unexpected Identifier.
Thanks in advance.
Edit:
node -v
Shows node is not defined ?? ..
I'm slightly confused.
Edit2: MAC IOS
Edit3: I installed it and the path was usr/local/bin am I allowed to just move the file?
Edit4: Thanks node -v works now. I opened it through the cmd tool
Edit5: okay now everything works.. I thought I have to work with the node cmd tool. Thanks for the quick help!
Open terminal
Go to the folder you have your example.js (use cd-command)
type in terminal node example.js, press enter
You should not RUN node before typing node example.js

Categories