Run Javascript file from command line - javascript

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

Related

I can't figure out how to run this simple program I found on GitHub

https://github.com/Valish/sherdog-api
After downloading it what I did was I first installed node.js, which I think I need in order to get everything for this (I have never used any of this stuff before at all). Then I went into command prompt and navigated to the place where the program im trying to use is. I then typed npm install and it installed all the files the program needs. I think that installed it correctly.
I then went into my code editor and tried to run the code through a browser. That didn't work because there is a function or something called "require" that I guess the browser doesn't have access to or anything.
Online, people seem to be saying that you run node.js programs through command prompt?
To test that I created a JavaScript file that has just this in it console.log('Hello');.
I then navigated to the file location and typed in command prompt "node hello.js" and it printed to the console so I know that works. What I don't know is it relevant that it works. This simple JavaScript file could have nothing to do with how the other program runs. I don't know.
Knowing that the simple file ran like this I did the same thing with the index.js file of the program I want. But when I do this nothing happens in the command prompt, it just brings up another command line. I can't figure it out.
On the GitHub site in the readme.md file for this program there is a "usage" section. However it doesn't explain how to use it as far as I can tell. It says
var sherdog = require('sherdog');
var url = "http://www.sherdog.com/fighter/Matt-Riddle-34072"
sherdog.getFighter(url, function(data) {
console.log(data);
})
If this is how you use it I don't know what to do with this, where to input it, or anything. There is no real documentation to go with it at all. I'm stuck at this point; I don't know where to learn or what to look at.
You need to type the sample code into a new .js file. sherdog-api is an api, which means it will not run any code by itself. Run your new .js file with node.
Node is a server running with JavaScript. It is mainly based on modules. A module just contains some code that helps you, most modules can't actually be run directly just like in your case. Instead you have to install the module, and then you can write code that works with it. At first make a new folder for your project, e.g. myCoolServer then open a command prompt in that folder and run:
npm init
To set up everything you need to run nodejs here, then run
npm install sherdog
To install the module. It will appear under /node-modules/. Now just create a new file like you did with hello.js, put the code into it:
var sherdog = require('sherdog');
var url = "http://www.sherdog.com/fighter/Matt-Riddle-34072";
sherdog.getFighter(url, function(data) {
console.log(data);
});
console.log("searching...");
And then run it using node thatfile.js, and you should see the data appearing after a while.
If you succeeded with that, you can continue here...

How to Run JavaScript file with node.js on windows (scribbletune)

Is it possible to run a JavaScript file with node.js on windows? I have been trying to for hours and can't find any more solutions on the internet that work.
I have a js-file that uses scribbletune which only works with node.js.
I have node.js installed and I installed gitbash because it was recommended in a forum.
I tried to run from command prompt and gitbash but nothing seems to happen.
What am I doing wrong? Any help would be very much appreciated.
There is no need to install gitbash to use Node on Windows. It's a handy thing to have if you're used to a *nix environment. If you're not, it just gives you something more to learn, which isn't helpful if you're already in the middle of trying to learn Node.
Just:
Get the Windows installer from https://nodejs.org/en/download/
Run the Windows installer
Create a directory for your project
Open a Command Prompt Window to get a command line
Switch to your project directory
(Optional, but a good idea) Use npm init to create a package.json file (it'll walk you through it)
Install any libs you're going to be using via npm (for instance, npm install scribbletune from your command prompt window)
Put your JavaScript files in that directory
Use node main.js at the command line to run your main file (whatever it's called; main.js is just a placeholder)
Inorder to run a js (java script file) file
step 1. u need to go to the file location where u want to run.
step 2. just use "shift +right click".
u will see a pop-up and go to powershell or cmd.
step 3. type "node FILENAME.js"
final step: you will see the result ^.^

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.

PhantomJS; Install and Running Examples

I am new to PhantomJS and to JavaScript. My question is very basic, but it has been giving me a lot of difficulty.
I have successfully added the PATH of the executable file to my system variables and when I type "phantomjs --version" into the command prompt, the correct version (1.7.0) is displayed.
However, when I try testing one of the examples, for instance hello.js, I receive the following message "Can't find variable: hello"
Originally I kept the examples files in the folder "examples" but have also tried moving them to the main phantomjs folder, where the executable file resides. Neither option works.
I also tried opening the examples using the following statement, from the command prompt: "phantomjs examples/hello.js" and I received the following statement "Can't open 'examples/hello.js'".
I know there have been a few other similar questions regarding this on the forum, but the solutions have not worked for me. Any fresh advice would be great, thanks!
Assuming hello.js file is in your current working directory, you need to type:
phantomjs hello.js
in your terminal or command prompt, and not in PhantomJS prompt.
Was having the same trouble. Finally solved it by specifying absolute path to phatnomjs executable.
instead of:
phantomjs myjsfile.js
do:
/my/absolute/path/to/phantomjs/phantomjs myjsfile.js
I suspect setting up PATH to it will have the same effect, have not tested it though.

phantomjs: command not found

I followed these instructions (except for copying the executable to my PATH because I cannot seem to find it and it does not seem necessary). Then I made a file called image_render.js in my public javascripts directory with
console.log('Hello, world!');
phantom.exit();
inside it, saved it, and ran phantomjs render_image.js in my terminal.
However, my terminal does not recognize the command:
-bash: phantomjs: command not found
What have I done wrong?
The PATH really is the important part. You can skip it, however, if you specify the absolute path. Try something like this:
/path/to/phantomjs render_image.js
...but that can get tiring. For a quick way to add it to your PATH, from the directory of the phantomjs executable, symbolically link it into /usr/local/bin:
sudo ln -s /path/to/phantomjs /usr/local/bin/
/usr/local/bin is likely on your PATH.
add this line to this file /home/optiman2/.bashrc
PATH=/home/optiman2/phantomjs/bin:$PATH
this worked for me.
and remember to use this command, before test phantomjs:
source .bashrc
FYI to Windows users upgrading to version 2.0.0 - the executable has moved. In addition to changing your PATH environment variable to 2.0.0, you'll need to add \bin to the end of the PATH that you had for the 1.x.x.
Mac PATH suggested setup:
Open Terminal.
Type vi ~/.bash_profile and hit enter (this opens or creates your bash_profile where you can customize Terminal commands).
Press i to enter insert/edit mode then type alias phantomjs='~/PATH/TO/phantomjs' and be sure to replace ~/PATH/TO/phantomjs with something like ~/Documents/PhantomJS/bin/phantomsj or wherever the file exists on your machine.
Press esc to exit insert/edit mode then type :x and hit enter to save the file.
Quit and re-open Terminal.
Type phantomjs and hit enter. If you see the prompt phantomjs> then you're all set.
Type phantom.exit() to end the PhantomJS program.
Now you can navigate to any folder and run PhantomJS by simply typing phantomjs. This is helpful when saving screenshots because the images will be saved inside the folder that's active in your Terminal.
Whatever command is just a executable file. To be accessible by type its name directly, you have to put it into a path that system will look for that file directly.
For linux/OSX, it's /usr/bin or /usr/local/bin. Which really works? Well, it depends...
So what worked for me is extract the 'phantomjs.tar.bz2' source file to somewhere and copy the executable file to /usr/local/bin like this:
cp path-of-extracted-phantomjs/bin/phantomjs /usr/local/bin

Categories