这篇文章将介绍一个开源项目markdown-pdf的使用,以及遇到的
一个无数个坑
前提
需要node.js
环境,没有的请自行百度安装node.js
安装markdown-pdf
这一条是官网的安装教程
解释一下参数,npm
是一个node.js
的包管理工具,-g
是全局安装,指安装后可以作为一个命令使用,--ignore-scripts
是忽略一些脚本(package.json
中的script
)
npm install -g markdown-pdf --ignore-scripts
但是跑着跑着如果发现报错,原因是权限不足,我们在linux和macOS环境下需要sudo
sudo npm install -g markdown-pdf --ignore-scripts
终于成功装好了,赶紧试用一个
jabin@jabin-deepin ~/Code/tmp [16:25:24]
> $ markdown-pdf lesson01.md
internal/validators.js:125
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object
at validateString (internal/validators.js:125:11)
at normalizeSpawnArguments (child_process.js:399:3)
at spawn (child_process.js:532:38)
at Object.execFile (child_process.js:221:15)
at WriteStream.<anonymous> (/usr/lib/node_modules/markdown-pdf/index.js:117:22)
at WriteStream.emit (events.js:194:15)
at finishMaybe (_stream_writable.js:641:14)
at stream._final (_stream_writable.js:619:5)
at WriteStream._final (internal/fs/streams.js:263:3)
at callFinal (_stream_writable.js:612:10)
又发现了错误!这个错误的解决方法就是先卸载已经装好的模块,去掉--ignore-scripts
重装一遍
- 卸载
sudo npm uninstall -g markdown-pdf
- 去掉参数重装
sudo npm install -g markdown-pdf
去掉参数之后,会被检测出来缺少一个模块phantomjs,然后脚本会自动安装,问题是,这个模块的下载速度奇慢无比,挂了10分钟甚至卡在8%不动了
jabin@jabin-deepin:~$ sudo npm install -g markdown-pdf
npm WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
/usr/bin/markdown-pdf -> /usr/lib/node_modules/markdown-pdf/bin/markdown-pdf
> phantomjs-prebuilt@2.1.16 install /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt
> node install.js
PhantomJS not found on PATH
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Receiving...
[===-------------------------------------] 8%
-
输出信息可以看到链接,尝试使用浏览器下载https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
发现速度非常慢,原因是中国特有的设施影响了我们的访问 -
可以看到脚本会下载资源,然后放在对应的路径,所以我们手动下载文件然后放在对应的路径,至于文件,你们可以到我的百度云下载,
-
linux、macOS、windows三版本汇总链接
链接: https://pan.baidu.com/s/1Ubxqe-RF_H_g5HJvc4CFmQ 提取码: qwbw -
将文件下载好,放在对应的路径(比如我的输出为
/tmp/phantomjs/
文件夹下)
经过测试,macOS和linux的路径都是
/tmp/phantomjs/
jabin@jabin-deepin /tmp/phantomjs [17:00:07]
> $ sudo npm install -g markdown-pdf
npm WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
/usr/bin/markdown-pdf -> /usr/lib/node_modules/markdown-pdf/bin/markdown-pdf
> phantomjs-prebuilt@2.1.16 install /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt
> node install.js
PhantomJS not found on PATH
Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Verified checksum of previously downloaded file
Extracting tar contents (via spawned process)
Removing /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64 -> /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom
Phantom installation failed { [Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64' -> '/usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom']
errno: -13,
code: 'EACCES',
syscall: 'link',
path:
'/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64',
dest:
'/usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom' } Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64' -> '/usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! phantomjs-prebuilt@2.1.16 install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the phantomjs-prebuilt@2.1.16 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-10-09T09_00_32_201Z-debug.log
发现还是报错,尽管sudo了,还是permission denied,这需要我们在后面加上参数--unsafe-perm
,执行sudo npm install -g markdown-pdf --unsafe-perm
jabin@jabin-deepin /usr/lib/node_modules [18:32:58]
> $ sudo npm install -g markdown-pdf --unsafe-perm
npm WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
/usr/bin/markdown-pdf -> /usr/lib/node_modules/markdown-pdf/bin/markdown-pdf
> phantomjs-prebuilt@2.1.16 install /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt
> node install.js
PhantomJS not found on PATH
Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Verified checksum of previously downloaded file
Extracting tar contents (via spawned process)
Removing /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570617196654/phantomjs-2.1.1-linux-x86_64 -> /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom
Writing location.js file
Done. Phantomjs binary available at /usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
+ markdown-pdf@10.0.0
added 106 packages from 381 contributors in 13.885s
jabin@jabin-deepin /usr/lib/node_modules [18:33:19]
> $
试用
jabin@jabin-deepin ~/Code/tmp [18:37:36]
> $ markdown-pdf lesson01.md
效果
md转pdf
下一篇会尝试更换主题~~
网友评论