【问题标题】:How to make npm package available as a command to electron application如何使 npm 包作为电子应用程序的命令可用
【发布时间】:2020-12-04 22:16:08
【问题描述】:

我在开发电子应用程序时遇到了一个奇怪的问题。我想要实现的是使用lighthouse 进行页面审核。我像这样以编程方式使用它

const command = `lighthouse ${website} --quiet --chrome-flags=--headless --output-path=${outputPath}   --output html --emulated-form-factor=${strategy}  --only-categories=${options}`;

os.execCommand(command, function() {
      res.send(response);
});

它的作用是在主线程(nodejs)中执行lighthouse 作为命令。我已经提到lighthouse 作为package.json 中的依赖项,它在development 模式下完美运行。

奇怪的是,当创建包时它给了我一个错误lighthouse is not a recognized command

如何解决这种依赖关系?或者提供来自node_modules>

的命令路径

请指导。 PS:无法使用require 将灯塔作为模块使用。此外,这是一个在内部托管此服务的独立应用程序。

【问题讨论】:

    标签: javascript node.js webpack electron lighthouse


    【解决方案1】:

    您能否启动一个子进程并通过npx 运行灯塔? 允许npx解析依赖空间而不是依赖os

    const {app, BrowserWindow} = require('electron')
    const childproc = require('child_process')
    const path = require('path')
    
    function createWindow () {
      // Create the browser window.
      const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
          preload: path.join(__dirname, 'preload.js')
        }
      })
    
      // and load the index.html of the app.
      mainWindow.loadFile('index.html')
    
      // Open the DevTools.
      mainWindow.webContents.openDevTools()
    }
    
    
    app.whenReady().then(() => {
      createWindow()
      
      app.on('activate', function () {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
      })
    })
    
    app.on('window-all-closed', function () {
      if (process.platform !== 'darwin') app.quit()
    })
    
    const child = childproc.spawn(
      'npx',
      [
        'lighthouse',
        `${website}`,
        `--quiet`,
        `--chrome-flags=--headless`,
        `--output-path=${outputPath}`,
        `--output`,
        `html`,
        `--emulated-form-factor=${strategy}`,
        `--only-categories=${options}`
      ],
    )
    
    child.on('exit', function (code, signal) {
      console.log(
        'child process exited with ' + `code ${code} and signal ${signal}`
      )
    })
    
    child.on('error', (err) => {
      console.log(err)
    })
    
    
    child.stdout.on('data', (data) => {
      console.log(data.toString())
    })
    
    child.stderr.on('data', (data) => {
      console.error(`child stderr:\n${data}`)
    })
    

    不太确定您的整个设置,但使用启动器构建了此示例并在本地运行灯塔...

    $ npm start
    
    > electron-quick-start@1.0.0 start 
    > electron .
    
    child stderr:
    Please provide a url
    
    child stderr:
    
    Specify --help for available options
    
    child process exited with code 1 and signal null
    

    【讨论】:

      【解决方案2】:

      您可以将 lighthouse 安装为全局依赖项。如果您不想这样做,请使用 npm bin 获取 npm 将安装可执行文件的文件夹。然后,使用灯塔的绝对路径。 https://stackoverflow.com/a/15157360/10674906 解释了如何做得更好。

      【讨论】:

      • 我尝试过的hacky解决方案是使用extraFiles属性在包中包含node_modues。根本不是一个好的解决方案。希望有更好的解决方案。
      • 我可以让它工作。我在全球范围内安装了灯塔,我正在使用来自child_process 模块的exec 来运行命令。另外,我在 Electron 的主进程中运行它。
      • 这对你自己有用。从应用的角度思考。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 2019-10-15
      • 2020-12-31
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多