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
Related
I just recently installed this version of Linux and when ever I do code fileName in the terminal it makes a whole new file. IS there a way for me to open an already existing code file I have?
Thanks for taking your time and effort to answer this question!
Yes, there is!
You need to be on the same folder of the file. For example, if the path of your file is /home/Downloads/ you need to use cd Downloads and then try to open the file. However, note that the code command that you are trying to use is the default command to open the file in the vscode. If you want to open the file using the default text editor in Linux, usually you have to write gedit filename in your terminal
Personally, I open the directory of the project and run command code .
This opens it with VSCode, but that's okay because most often I will need all other files anyways. Brackets is brackets .
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
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 ^.^
I just started experimenting with phantomjs via terminal/bash Version 2.5.3 (343.7), on osx 10.10.4. The ready to use binary was installed.
I was able to set phantomjs on my PATH with symbolical linking. Prior to that I had to directly launch the executable or enter its absolute path.
When I try to launch a .js file with phantomjs, for example hello.js, I get " Can't open 'hello.js' ". If I enter the absolute path of the .js file it works:
console.log('Hello, world! It works :D');
phantom.exit();
Johns-MacBook-Pro:~ john$ phantomjs hello.js
Can't open 'hello.js'
VS
console.log('Hello, world! It works :D');
phantom.exit();
Johns-MacBook-Pro:~ john$ phantomjs /Users/john/Documents/phantomjs-1.9.2-macosx/examples/hello.js
Hello, world! It works :D
Johns-MacBook-Pro:~ john$
My question is: Do I need to symbolically link the folder where the .js files
are located with /usr/local/bin/ to ensure that it is also on my PATH or is there another way that
the .js files can be launched with phantomjs, without having to enter the absolute path every time?
I did search before posting this question but I'm uncertain as to how I should interpret everything I found :( The most significant, and somewhat relevant information I found referenced to node.js as the default to execute .js files in terminal.
Please note that currently, the hello.js file I am using is located in
/Users/john/Documents/phantomjs-1.9.2-macosx/examples/hello.js
and phantomjs is located in
/Users/john/Documents/phantomjs-1.9.2-macosx/bin/phantomjs
Please let me know if you need further information and thank you for taking the time to read my question! :)
You need to be in the folder that the file is in to run it directly. If you are in the folder /Users/john/Documents/phantomjs-1.9.2-macosx/examples/ you should be able to run phantomjs hello.js.
# Change directory to where the file is you want to run in phantom
cd /Users/john/Documents/phantomjs-1.9.2-macosx/examples/
# Run the file with phantom
phamtomjs hello.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.