Nodejs中PHANTOMJS明明是有的啊,npm install怎么报错了?
Nodejs中PHANTOMJS明明是有的啊,npm install怎么报错了?
npm install -d .
npm http 404 http://registry.npm.taobao.org/phantomjs/1.9.7 npm ERR! 404 ‘phantomjs’ is not in the npm registry. npm ERR! 404 You should bug the author to publish it npm ERR! 404 It was specified as a dependency of 'Brackets’ npm ERR! 404 npm ERR! 404 Note that you can also install from a npm ERR! 404 tarball, folder, or http url, or git url.
npm ERR! System Windows_NT 6.1.7601 npm ERR! command “C:\Program Files\nodejs\\node.exe” “C:\Program Files\nod ejs\node_modules\npm\bin\npm-cli.js” “install” “-d” "." npm ERR! cwd E:\projects\web\del\brackets npm ERR! node -v v0.10.25 npm ERR! npm -v 1.3.24 npm ERR! code E404 npm ERR!
E:\projects\test>phantomjs phantomjs>
针对您提供的问题,我们可以从以下几个方面来分析和解决这个问题:
问题分析
从错误信息来看,npm install
命令尝试安装 phantomjs
包时遇到了 404 错误。这意味着在 npm 的官方仓库或指定的镜像源(如淘宝镜像)中找不到该包。
解决方案
1. 检查包名是否正确
首先,确保你输入的包名是正确的。phantomjs
这个包名已经过时了,你应该使用 phantomjs-prebuilt
或者更推荐的是 puppeteer
,后者是一个由 Google Chrome 团队维护的现代解决方案。
npm install --save puppeteer
2. 使用正确的包
如果你确实需要使用 phantomjs
的功能,可以考虑使用 puppeteer
或者 phantomjs-prebuilt
。
使用 Puppeteer 示例
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://example.com');
await page.screenshot({path: 'example.png'});
browser.close();
})();
使用 Phantomjs-prebuilt 示例
npm install --save phantomjs-prebuilt
然后你可以这样使用:
var phantom = require('phantom');
(async function() {
var instance = await phantom.create();
var page = await instance.createPage();
var status = await page.open('http://google.com');
console.log(status);
console.log(await page.property('title'));
await instance.exit();
})();
3. 检查 npm 配置
确保你的 npm 配置正确,并且没有指向一个不存在的仓库。你可以通过以下命令检查和重置配置:
npm config get registry
npm config set registry https://registry.npmjs.org/
通过以上步骤,你应该能够解决 npm install
报错的问题,并成功安装所需的包。
应该是npm的时候,没有找到phantomjs npm http 404 http://registry.npm.taobao.org/phantomjs/1.9.7
E:\projects\test>phantomjs phantomjs> 这只是表示你在本机上安装phantomjs了
可以在js这样使用通过spawn来调用phantomjs var phantomjsPath = ‘/opt/phantomjs-1.7.0-macosx/bin/phantomjs’; var rasterizePath = ‘/public/javascripts/rasterize.js"’; var phantomjs = spawn(phantomjsPath, [rasterizePath, ‘http://marshal.easymorse.com’, ‘xxx.jpg’]); phantomjs.on(‘exit’, function (code) { console.log(‘截图成功后,读取图片的二进制数据’); console.log('截图 code: ', code);
当然使用mudel更方便
});
根本就没有 1.9.7版本, 只要1.9.7-15 http://registry.npm.taobao.org/phantomjs/1.9.7-15