第一篇札记记录一个简单 npm 包安装问题。
现象
小伙伴使用 gitbook 构建组内的一个文档库,在安装 gitbook-pdf
时遇到了如下问题:
➜ test npm install gitbook-pdf
npm WARN deprecated ini@1.1.0: Please update to ini >=1.3.6 to avoid a prototype pollution issue
npm WARN deprecated npmconf@0.0.24: this package has been reintegrated into npm and is now out of date with respect to npm
npm WARN deprecated mkdirp@0.3.5: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm ERR! code 1
npm ERR! path /Users/xxx/Desktop/Dev/test/node_modules/phantomjs
npm ERR! command failed
npm ERR! command sh -c node install.js
npm ERR! Downloading http://cdn.bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip
npm ERR! Saving to /var/folders/w0/0447kss56817yrbrmxcsw9fw0000gn/T/phantomjs/phantomjs-1.9.7-macosx.zip
npm ERR! Receiving...
npm ERR! Error requesting archive.
npm ERR! Status: 301
npm ERR! Request options: {
npm ERR! "protocol": "http:",
npm ERR! "slashes": true,
npm ERR! "auth": null,
npm ERR! "host": "cdn.bitbucket.org",
npm ERR! "port": null,
npm ERR! "hostname": "cdn.bitbucket.org",
npm ERR! "hash": null,
npm ERR! "search": null,
npm ERR! "query": null,
npm ERR! "pathname": "/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip",
npm ERR! "path": "/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip",
npm ERR! "href": "http://cdn.bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip"
npm ERR! }
npm ERR! Response headers: {
npm ERR! "content-type": "text/html",
npm ERR! "content-length": "0",
npm ERR! "connection": "close",
npm ERR! "date": "Fri, 15 Apr 2022 04:26:29 GMT",
npm ERR! "location": "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip",
npm ERR! "x-cache": "Hit from cloudfront",
npm ERR! "via": "1.1 dccfa316bb1b94c6fd9cede16dd6ab38.cloudfront.net (CloudFront)",
npm ERR! "x-amz-cf-pop": "HKG60-C1",
npm ERR! "x-amz-cf-id": "AFFPOkQCZYivoc_NLRA2i2dGbWJ4K2ImH3bIqBpQJPCU32m1jXkFLQ==",
npm ERR! "age": "3938"
npm ERR! }
npm ERR! Make sure your network and proxy settings are correct.
解决
gitbook-pdf 是一个很老的包了,距今已有 8 年历史。npm 包介绍这里的 github repo 地址已经没有了。其实应该有其他替代包了(未求证)。
根据报错信息可以看出是在安装其依赖包 phantomjs
出现的。猜测是这个包在执行node install.js
因为里面的逻辑写的不是很好,将非 200
的请求直接抛错了。
分析
- 查看
gitbook-pdf
的依赖包,可以查看到其依赖的是phantomjs: 1.9.7-5
npm view gitbook-pdf
- 准备看下
phantomjs: 1.9.7-5
里面具体的install.js
的逻辑,所以需要先下载一下 npm pkg 的source code
npm view phantomjs@1.9.7-5
wget 下载一下,然后分析 install.js
的逻辑。
install.js
逻辑分析 这里我们只关注其中下载逻辑。
逻辑和我们猜的类似,非 200 直接报错退出了。
具体原因是 http://cdn.bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip
这个地址被做了重定向,返回301了。新的地址是https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip
然后关注到其中有检查本地 tmp 目录中文件是否存在的逻辑,如果有就不进入下载逻辑。
所以如果仍然想安装,可以把这个 zip 文件先现在下来,找到上文报错的保存路径。
然后进入/var/folders/w0/0447kss56817yrbrmxcsw9fw0000gn/T/phantomjs/
,使用wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-macosx.zip
将 zip 下载下来。
再重新npm install gitbook-pdf
可以发现安装成功了。
其他
- npm package.json script
{
"scripts" : {
"install" : "scripts/install.js",
"postinstall" : "scripts/install.js",
"uninstall" : "scripts/uninstall.js"
}
}
scripts/install.js will be called for the install and post-install stages of the lifecycle, and scripts/uninstall.js will be called when the package is uninstalled. Since scripts/install.js is running for two different phases, it would be wise in this case to look at the npm_lifecycle_event environment variable.
- npm view
This command shows data about a package and prints it to stdout.
查看包的相关信息,比如 source code 地址、依赖等等。
https://docs.npmjs.com/cli/v8/commands/npm-view