我已经创建了一个小命令行程序,使用npm,puppeteer和commander将html文件转换为pdfs .

我在开发它时使用过

npm link

将我的开发代码链接到命令而不是全局安装的代码 . 这非常有效:

pdfer infile.htm --out outfile.pdf

创建PDF . 但是,当我取消链接时,将其全局安装

npm unlink
sudo npm install -g
pdfer infile.htm --out outfile.pdf

这是行不通的 . 它会在下面的注释行中退出并显示以下消息:

(node:28553) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: spawn EACCES
(node:28553) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

如果我做

nodejs <path>/index.js infile.htm --out outfile.pdf

这对我有用;如果我作为一个不同的用户尝试它,它不起作用;所以它可能是一个环境问题 . 最后,我真正需要的只是系统上所有用户都可以运行的东西 .

知道我可能做错了什么吗?

该计划是:

#!/usr/bin/env node

// Load packages that we need
const c = require('commander');
const p = require('puppeteer');

c.arguments('');

var args = {
    out: {
        pass: true,
        configName: 'path',
        desc: 'Output file name.'
    }
};

for(var i in args){
    c.option('--'+i+' ',args[i]);
}
c.action(function(file){

    // Prepare the config object
    var config = {};

    // Get the things that pass through.
    for(var i in args){
        var obj = args[i];
        if(obj.pass && c[i]){
            var outKey = obj.configName || i;
            config[outKey] = c[i];
            console.log(i+' = '+c[i]);
        }
    }

    // Get the page to create the PDF.
    (async () => {
        console.log('made it');
        const browser = await p.launch(); // The program dies on this line when run globally.
        console.log('didnt make it here');
        const page = await browser.newPage();
        await page.goto('file://'+file, {waitUntil: 'networkidle2'});
        await page.pdf(config);
        await browser.close();
    })();

}).parse(process.argv);

我的package.json是

{
  "name": "pdfer",
  "version": "0.1.0",
  "description": "PDF HTML files/web pages",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "pdfer": "./index.js"
  },
  "dependencies": {
    "commander": "^2.13.0",
    "puppeteer": "^1.0.0"
  },
  "author": "me",
  "license": "Apache 2.0"
}