PhantomJS; Install and Running Examples - javascript

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.

Related

Beginner having trouble running Javascript on Visual Studio Code

New coder here. As the title suggests, I spent the past two hours trying to run a simple Javascript on VSC with no avail. Could someone help me set my sandbox up? Here is a screenshot.
Much appreciated!
kt
Downloaded VSC
Downloaded nodejs
Entered "node scriptname.js" in terminal
Error message above
Your specific error is caused by you running node from the console and providing the wrong path to test.js.
You are in the Desktop directory and just specifying a file name, so Node is looking for test.js in the Desktop directory.
You have saved test.js in a directory called Coding Practice.
You need to either:
cd to the correct directory or
Provide the path to the directory as part of the second argument
Typically, when using VS Code, you would pick the Open Folder option from the File menu to work within your project's root directory (i.e. Coding Practice) which will provide you with a file list and do things like open the terminal in that directory by default.
Once you solve that problem you will run into your second problem.
The contents of test.js isn't JavaScript!
It's an HTML document with JavaScript embedded in it.
You need to:
Give it a .html file extension and
Open it using a web browser and not with Node.js (the traditional way to do that from within VS Code is with the live server extension but you'll really want to have VS code open in the right directory (as above) for that.)
You can't even remove the HTML from the file and run it with Node.js because alert (the function you call) is a Web API and not core JavaScript nor a Node.js API.

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

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

zapier-platform-cli init Error! ENOENT: no such file or directory

Just starting with a Zapier Integration development using zapier-platform-cli, though after a simple zapier init zapp I am getting the following output:
Error! ENOENT: no such file or directory, open C:\Users\USERNAME:\Users\username1\AppData\Local\Temp\tmp-286764LYpWKGFgiNI\zapier-template.zip'
It seems that the command is trying to access a wrong path. I am using Windows 7 and this seems to be a reported/open issue in the repository zapier-platform-cli issue.
So, just trying to know if someone has any thoughts or idea on how to fix this.
It was a problem with the TMP Environment variable.
I have solved the problem setting up the variable to C:\Users\USERNAME\AppData\Local\Temp
Also, I did the same to TEMP variable as it is used instead of TMP in some situations.
As I am using Powershell I have made it permanent by adding to my Powershell profile (C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1) the following lines:
[Environment]::SetEnvironmentVariable("TEMP", "C:\Users\USERNAME\AppData\Local\Temp")
[Environment]::SetEnvironmentVariable("TMP", "C:\Users\USERNAME\AppData\Local\Temp")
That is a known issue as you correctly pointed out https://github.com/zapier/zapier-platform-cli/issues/1.
It might be related to underlying zip libraries but we've not had the chance to test it.
Have you tried zapier build --debug and pasting the traceback on the issue as a comment?

Phantom js returns 'can't open [file name]' when trying to run example js file

Environment: Windows 7, Phantomjs version 1.8.0.
I try to run any .js file from 'examples' folder, installed Phantom.js before, added path to the PATH variable, checked version like
phantomjs --version
and it works.
But when I try to run
phantomjs examples/version.js
then get 'Can't open version.js'
I've checked file security settings and run cmd like administrator, but still the same result.
I'll appreciate any help to solve this issue. Thanks in advance.
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.
In my case,
/mypath/phantomjs /mypath/myfile.js
It takes three days for finding this out.
Pretty wired syntax to me.
If it doesn't work then use . in front of first /
./mypath/phantomjs ./mypath/myfile.js
. means base dir as you set in the R studio
In my case I was just not in the right folder, make sure of being in the exact same folder as "myfile.js"

Categories