PhantomJS 安裝

AZmake發表於2017-04-11

PhantomJS 安裝

圖片描述

Mac OS X && Windows

1、PhantomJS下載地址

按照系統下載對應的版本,

macOS 下載: phantomjs-2.1.1-macosx.zip

Windows下載:phantomjs-2.1.1-windows.zip

並解壓到使用者目錄下面,

macOS 解壓路徑:

/User/xxx/phantomjs-2.1.1-macosx

Windows 解壓路徑:

D:\phantomjs-2.1.1-windows ,並重新命名為 D:\phantomjs

2、PhantomJS不需要安裝,解壓即可使用

Windows PATH變數

在“系統變數”選項區域中檢視PATH變數,如果不存在,則新建變數 PATH,

否則選中該變數,單擊“編輯”按鈕,在“變數值”文字框的起始位置新增 

D:\phantomjs\bin,開始-執行-輸入cmd,輸入 phantomjs --version, 
 
如果可以看到版本號,則安裝成功。

macOS PATH變數


如果使用者目錄下存在 .bash_profile ,比如我的/User/xxx/.bash_profile,則新增一行 

export PATH="$PATH:/Users/xxx/phantomjs-2.1.1-macosx/bin"

否則新建 .bash_profile,然後在新增上文

在terminal中輸入

phantomjs --version

如果可以看到版本號,則安裝成功。

3、使用npm安裝的時候如何環境變數中沒有phantomjs,就會從github下載安裝phantomjs,但是你懂的?,大多數情況下會超時,即使把npm的映象改成淘寶的還是一樣的結果

PhantomJS not found on PATH
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-macosx.zip
Saving to /var/folders/hn/55rkvgy97797tgx6cld83zl80000gn/T/phantomjs/phantomjs-2.1.1-macosx.zip
  • 然後聰明的你想到可以使用 npm install -g phantomjs , 可是現實告訴你

npm WARN deprecated phantomjs@2.1.7: Package renamed to phantomjs-prebuilt. Please update 'phantomjs' package references to 'phantomjs-prebuilt'
npm WARN deprecated tough-cookie@2.2.2: ReDoS vulnerability parsing Set-Cookie https://nodesecurity.io/advisories/130
npm WARN deprecated node-uuid@1.4.8: Use uuid module instead
/usr/local/bin/phantomjs -> /usr/local/lib/node_modules/phantomjs/bin/phantomjs

> phantomjs@2.1.7 install /usr/local/lib/node_modules/phantomjs
> node install.js

Considering PhantomJS found at /usr/local/bin/phantomjs
Looks like an `npm install -g`
Error checking path, continuing { Error: Cannot find module '/usr/local/lib/node_modules/phantomjs/lib/location'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at getLocationInLibModuleIfMatching (/usr/local/lib/node_modules/phantomjs/install.js:332:19)
    at Promise._successFn (/usr/local/lib/node_modules/phantomjs/install.js:389:28)
    at nextTickCallback (/usr/local/lib/node_modules/phantomjs/node_modules/kew/kew.js:47:28)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9) code: 'MODULE_NOT_FOUND' }
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1//phantomjs-2.1.1-macosx.zip
Saving to /usr/local/lib/node_modules/phantomjs/phantomjs/phantomjs-2.1.1-macosx.zip
Receiving...
  • 又回到了原點,是不是npm的問題?我用cnpm試一下看看

  • 然後聰明的你想到可以使用 cnpm install -g phantomjs,現實是這樣

Downloading phantomjs to /usr/local/lib/node_modules/phantomjs_tmp
Copying /usr/local/lib/node_modules/phantomjs_tmp/.2.1.7@phantomjs to /usr/local/lib/node_modules/phantomjs
Installing phantomjs's dependencies to /usr/local/lib/node_modules/phantomjs/node_modules
[1/8] progress@~1.1.8 installed at node_modules/.1.1.8@progress
...
[8/8] request@~2.67.0 installed at node_modules/.2.67.0@request
execute post install 1 scripts...
[1/1] scripts.install phantomjs@2.1.7+deprecated run "node install.js"
PhantomJS not found on PATH
Phantom installation failed TypeError: Path must be a string. Received undefined
    at assertPath (path.js:7:11)
    at Object.join (path.js:1211:7)
Install fail! Error: post install error, please remove node_modules before retry!
Run "sh -c node install.js" error, exit code 1
Error: post install error, please remove node_modules before retry!
Run "sh -c node install.js" error, exit code 1
    at ChildProcess.proc.on.code (/usr/local/lib/node_modules/cnpm/
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
npminstall version: 2.26.2
npminstall args: /usr/local/bin/node /usr/local/lib/node_modules/cnpm/node_modules/npminstall/bin/install.js --china --userconfig=/Users/azmake/.cnpmrc --disturl=https://npm.taobao.org/mirrors/node --registry=https://registry.npm.taobao.org -g phantomjs
  • 看到提示

Install fail! Error: post install error, please remove node_modules before retry!
  • 什麼鬼?不陪你玩了

sudo npm uninstall -g phantomjs

sudo cnpm uninstall -g phantomjs

sudo rm -rf /usr/local/lib/node_modules/phantomjs
  • 然後你又想到可以使用HomeBrew安裝

brew install phantomjs
  • ok,安裝完成,開啟terminal,輸入 phantomjs --version,可以看到版本號,一起正常,可以愉快的玩耍了,然後你又愉快的開始 npm install 了,然後你又發現 使用phantomjs的專案裡面 could not verify phantomjs,然後

Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1//phantomjs-2.1.1-macosx.zip
Saving to /usr/local/lib/node_modules/phantomjs/phantomjs/phantomjs-2.1.1-macosx.zip
Receiving...

總結

1、可以使用cnpm install 安裝使用phantomjs的專案的node_modules,cnpm 會 使用phantomjs-prebuilt代替phantomjs,並且不中途翻車

2、下載安裝phantomjs,並配置環境變數,這樣不管是cnpm install還是npm install都可以使用phantomjs,而且在pycharm裡面也不會報找不到phantomjs的錯誤

相關文章