Unexpected command line syntax error in node js - javascript

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.

Related

Run function in running process on node js using command line

I would like to know if it's possible to run a specific function on a running process on Node JS. And how to do this
actually im using
npx run-func server.js functionName
but it completely restarts the file.
For information, I can't split the file in two since I use a database that can only be read by one process. So... If you have any advice please

Getting "'node' is not recognized" error when running JavaScript with Code Runner in VS Code

After I installed Code Runner in VS Code then used it to run my JS code, I'm getting this runtime error:
'node' is not recognized as an internal or external command,
operable program or batch file.
What does that mean?
What also helped me was (after installing node) going into VS settings, finding
code-runner.runInTerminal
and ticking it on.
The error message means you have not installed Node.js on your machine. Make sure you have installed the Node.js from https://nodejs.org/en/, and set the correct PATH environment variable.

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 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.

Load javascript from file in nodejs

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)

Categories