简体   繁体   English

node.js 编译的 pkg 不会运行(但从节点运行)

[英]node.js compiled pkg won't run (but does from node)

I have a simple program I am working on with node.js, the idea was to fetch some websites and show a report, it is for testing web content filtering in a company.我有一个使用 node.js 开发的简单程序,其想法是获取一些网站并显示报告,用于测试公司的 Web 内容过滤。

I started struggling with taking the screenshots of the website, when I run my code as node index.js it works perfectly, it outputs 'google.png' and contains a screenshot of the website (Just as in the screenshot-stream example).我开始努力获取网站的屏幕截图,当我将我的代码作为node index.js运行时,它运行良好,它输出“google.png”并包含网站的屏幕截图(就像在屏幕截图流示例中一样)。 However, I compile it using npx pkg index.js -t linux and it starts giving me errors on execution.但是,我使用npx pkg index.js -t linux编译它,它开始给我执行错误。

I reduced the program to only this:我将程序简化为:

const fs = require('fs')
const screenshot = require('screenshot-stream');

const stream = screenshot('http://google.com', '900x180', {crop: true});

stream.pipe(fs.createWriteStream('google.png'));

I run node index.js and it generates the image.我运行node index.js并生成图像。 However after compiling (and adding the phantomjs to the phantom folder as warned by npx), I get an error:但是,在编译后(并将 phantomjs 添加到 npx 警告的 phantom 文件夹中),我收到一个错误:

./index 
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: Can't open '/snapshot/screenshots/node_modules/screenshot-stream/stream.js'
    at LineStream.module.exports.byline.on.data (/snapshot/screenshots/node_modules/screenshot-stream/index.js:77:16)
    at emitOne (events.js:116:13)
    at LineStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at LineStream.Readable.push (_stream_readable.js:208:10)
    at LineStream.Transform.push (_stream_transform.js:147:32)
    at LineStream._pushBuffer (/snapshot/screenshots/node_modules/byline/lib/byline.js:125:17)
    at LineStream._transform (/snapshot/screenshots/node_modules/byline/lib/byline.js:116:8)
    at LineStream.Transform._read (_stream_transform.js:186:10)

I am using screenshot-stream@4.2.0, npm@6.4.1, pkg@4.3.4我正在使用 screenshot-stream@4.2.0、npm@6.4.1、pkg@4.3.4

Any help is greatly appreciated任何帮助是极大的赞赏

Debugged over this code and found that screenshot-stream module uses调试了这段代码,发现 screenshot-stream 模块使用了

path.join(__dirname, 'stream.js')

to pass as an argument to call phantomjs as a child process.作为参数传递以将 phantomjs 作为子进程调用。

When runing directly on node this works fine but when compiled with pkg, this child process has not access to the snapshot filesystem created by this packager.当直接在节点上运行时,这工作正常,但是当使用 pkg 编译时,这个子进程无法访问由这个打包器创建的快照文件系统。

I found a workaround: modifying screenshot-stream/index.js:我找到了一个解决方法:修改 screenshot-stream/index.js:

On line 45:在第 45 行:

Replace __dirname with './' and place stream.js on the binary working directory and should work fine.'./'替换__dirname并将 stream.js 放在二进制工作目录中,应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM