Load javascript from file in nodejs - javascript

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)

Related

Unexpected command line syntax error in node js

I have been trying to run some node js files. I have checked the files and they seem to have no syntax errors whatsoever. However, upon running the file on the node js command line i get this error
node first.js
^^^^^
SyntaxError: Unexpected identifier
Lately, I have also been unable to run my apache web server from XAMPP. Could this be a port assignee error. Or is it something else. I have also, checked the directory of the file and it checks out
Some help would be greatly appreciate
When you're inside node's CLI, you can run JS or rather node supported instructions. Unfortunately, there is no global variable or keyword with name "node". If you want to run "first.js", you'll have to come out from node's cli and then run the command.
You can't run file with node first.js inside file js
node is undefined
That is't right syntax
If you want run first.js file, you can include it with require
let first = require('first);
but this way run it once (first time call it), you can use method inside first.js with export(or module.exports) of first.js.
But you just want call and run first.js, you can use child_process.

Run Javascript file from command line

I have a javascript file hello.js with console.log("Hello World"). I want to run it from my terminal (I am on a mac). I have node installed and I take the following steps -
Open terminal and navigate to the directory where hello.js exists.
Run command "node hello.js"
But I dont see the console statement (Hello World) in my terminal.
Can some one please help me run javascript from terminal (or tell me what I am doing wrong) ?
PS: I have checked this thread but my problem still exists.
One possible error is that your JavaScript file doesn't end with a new line character.
The node parser will read the last line, but it won't completely process the line unless it includes a new line marker (or, possibly, a semicolon).
Make sure your file includes the new line (as well as, preferably, a semicolon), i.e.:
console.log("Hello World");
// EOF comment, to validate a new line marker after last line of code.
This might solve your issue - unless the reason for your issue lies somewhere else.
Good luck!
You have to first accurately create the path to the .js file. This is probably your problem.
When you first open the CLI (Command Line) there will be a path displayed.
For example...
C:\Users\yourname>
from there you have to get to where the file is located, so you type in..
C:\Users\yourname>cd\Users\yourname\Documents\course\jsfolder
cd means "change directory"..and just put the full path to where the file is located.
Now to see if you are there by typing dir on the end
C:\Users\yourname>\Documents\course\jsfolder>dir
This will list all the files in that last directory. You should see your New.js file in there. You are now ready to go!
Follow what Anupam said above...type.. node New.js ..and your file will run if you have node.js installed on your computer..BINGO!>
one last thing ..I believe the command "node" is "$node" on an Apple
There are a few additional solutions you could use other than node.
The expected behavior of node would be to print "Hello World" for you, and there has been help troubleshooting in the comments of your post.
jsc is included with macOS. You should make a link to the file first as described in this post, and change your console.log() methods to debug().
rhino is available from Mozilla. It would require you to switch your console.log() methods to print().
For installing rhino, you may be able to avoid some of the issues you've had with node by installing through homebrew. As a last means of troubleshooting node, you may also find it useful to reinstall it through homebrew.
You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName.js”.
Open Terminal or Command Prompt.
Set Path to where File is Located (using cd).
Type “node New.js” and Click Enter

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.

How to execute a hello world javascript file in node.js

I have hello_world.js file located on my computer at the following location -
C:\Users\Ankur\Downloads\NodeJS\working
It has the following code-
console.log("Hellow World");
I was reading a beginners tutorial here and it says I should execute my javascript file through node.js. Now I have no idea what to do. How would I do this.
When I do this ...
I get nothing.
What should I be doing to run the hello world successfully. None of the tutorials seem to be showing what needs to be done.
I am very new to this so it is probably something that is very obvious to you guys.
Use Node.js command prompt, then type in node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js
looks like you are in a node console already. you typed node which correctly started node (so your path is fine). Now you are telling node to interpret
node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js
as javascript which it doesn't like.
You should be able to type console.log('hello world'); here and see it appear.
To run your file, quit out of the node interpreter (i think control-X, maybe control-C), and at your C:> prompt, THEN type
node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js
Alternately,
cd C:\Users\Ankur\Downloads\NodeJS\working\
node hello_world.js
You have entered the node console, by typing node into a command prompt, and then tried to execute node from within itself. Just type node c:\etc\...\ from the command prompt, not the node shell.
Press: [Start]->[Run]->[c][m][d]
And enter command: node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js
Alternatively, use sublime editor, and install a node js build system, and just open the file you want to run, and press the build shortcut, getting the output in the console.
Open CMD.
Type "cd C:\Users\Ankur\Downloads\NodeJS\working" and hit Enter
Type "node hello_world.js" and hit Enter
This should work out !!
IMPORTANT: Dont type node before the point number 3.
Follow these three easy steps:
Go to your working directory
C:\Users\Ankur\Downloads\NodeJS\working
Open cmd there and type
node hello_world
Press Enter
Alternative.
Open Nodejs "The thing with a green icon".
Copy the code that you would like to be executed from the file that you have
paste it in nodejs then press enter
nodejs understands js code so it will execute what you have written and output
hello world
undefined
What you have descripbed opens the file that you have and then executes it.
Open Windows cmd and type
node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js
You should use node from system interpreter (bash/DOS cmd), what you are running is C:\Program Files\nodejs\node.exe without any arguments.

Categories