简体   繁体   English

使用 pkg 打包为可执行文件时,Node JS cli 脚本崩溃

[英]Node JS cli script crashing when packaged to a executable with pkg

A very simple script for web automation for filling out a form using puppeteer, works perfectly in my IDE when I run it with node filename.js一个非常简单的 web 自动化脚本,用于使用 puppeteer 填写表单,当我使用node filename.js运行它时,它在我的 IDE 中完美运行

But when I package it into a executable with pkg , it crashes on the first instance of input without any error being thrown, I don't expect a direct solution to this issue but maybe someone had a similar experience and can tell me what might be causing this, as I have no idea what to do right now.但是当我用pkg它打包成一个可执行文件时,它在第一个输入实例上崩溃而没有抛出任何错误,我不希望直接解决这个问题,但也许有人有类似的经历,可以告诉我可能是什么造成这种情况,因为我现在不知道该怎么做。

The code:代码:

    const puppeteer = require('./node_modules/puppeteer')

const parse = require('csv-parse');
const fs = require('fs');
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const data = [];

menu()

async function menu() {
    console.log("Please select a option\n\n" +
    "1. The Streets raffle\n" +
    "2. Footshop raffle\n" +
    "3. Exit");
    rl.question("", async function(answer) {
        console.log(`user choice: ${answer}`);

        if(answer === "1") {
            console.log("streets module starting");
            await streets_module();
        }
        else if(answer === "2") {
            console.log("In development, press enter to exit");
            rl.question("", async function(answer) {
            });
            rl.close;
            process.exit(0);
        }
        else if(answer === "3"){
            console.log("Now exitting...");
            rl.close;
            process.exit(0);
        }
        else {
            console.log("undefined choice, exitting...");
            rl.close;
            process.exit(0);
        }
        
    });
}

async function streets_module() {
    fs.createReadStream("profiles.csv") // this part can be omitted and instead the array from below can be assigned to the data variable
    .pipe(parse({ delimiter: ',' }))
    .on('data', (r) => {
        //console.log("r: ", r);
        data.push(r);        
    })
    .on('end', async () => {
        //console.log("data: ",data);
        //console.log("data at 1: ", data[1][0]); // prvy profil

        console.log("data length: ", data.length, data);

        for (let ii = 1; ii < data.length; ii++) {
            //console.log("this triggered");
            //C:\chrome-win\chrome.exe
            //console.log("async block triggered");
            console.log(`profile ${ii} started`)
            const browser = await puppeteer.launch({ignoreDefaultArgs: ['--disable-extensions']});
            const page = await browser.newPage();
            await page.goto('https://www.thestreets.sk/online-raffle/');
            await page.type("#name", data[ii][0]); // ,{ delay: 100 });
            await page.type("#yourEmail", data[ii][1]);//, { delay: 100 });
            await page.type("#phone", data[ii][2]);//, { delay: 100 });
            await page.type("#street", data[ii][3]);//, { delay: 100 });
            await page.type("#city", data[ii][4]);//, { delay: 100 });
            await page.type("#psc", data[ii][5]);//, { delay: 100 });
            await page.select('select#state', data[ii][6]);//, { delay: 100 }); 
            await page.select('select#prefered_size_sel', data[ii][7]);//, { delay: 100 });
            await page.$eval('input[name="agreed_personal_info_tiny_contact_form"]', check => check.checked = true);
        
        
            await page.screenshot({ path: 'streets' + ii + '.png' });
        
            await page.click('input[name="submit"]');
            
            //console.log("async block finished");
            console.log(`profile ${ii} finished`);
            await browser.close();
        }
        console.log("all profiles traversed, closing...");
        process.exit(0);
    })
    
}

The csv file holds some testing data to see if the form gets filled out correctly, here's the data in a array for testing purposes: csv 文件包含一些测试数据以查看表单是否正确填写,以下是用于测试目的的数组中的数据:

    [
  [
    'Full name',
    'Email',
    'Phone number',
    'Street',
    'City',
    'Postal code',
    'State',
    'Size'
  ],
  [
    'test',
    'test@test.com',
    '090123456789',
    'ulica',
    'Presov',
    '10902',
    'SVK',
    '5W'
  ],
  [
    'test 2',
    'test2@test.com',
    '090123452789',
    'ulica2',
    'Presov2',
    '10903',
    'SVK',
    '6W'
  ]
]

A video of the issue: a video showcase of the issue问题的视频:问题的视频展示

Thank you for getting this far and for any possible suggestions as to why the exe file is crashing :)感谢您走到这一步,并就 exe 文件崩溃的原因提出任何可能的建议:)

尝试用绝对路径替换 csv 文件名,以防被访问的目录不同

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

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