phantomjs: command not found

2019-01-16 14:22发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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.



回答4:

Mac PATH suggested setup:

  1. Open Terminal.
  2. Type vi ~/.bash_profile and hit enter (this opens or creates your bash_profile where you can customize Terminal commands).
  3. 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.
  4. Press esc to exit insert/edit mode then type :x and hit enter to save the file.
  5. Quit and re-open Terminal.
  6. Type phantomjs and hit enter. If you see the prompt phantomjs> then you're all set.
  7. Type phantom.exit() to end the PhantomJS program.
  8. 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.


回答5:

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