简体   繁体   English

NPM 和 NodeJS 兼容性:NodeJS 在 PM 提示下工作,但不是脚本

[英]NPM and NodeJS Compatibility: NodeJS works from PM prompt, but not script

I am attempting to get a lighthouse script running in Node.JS (which I am new to).我正在尝试在 Node.JS 中运行一个灯塔脚本(我是新手)。 I followed the intial instructions here https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically .我按照这里的初始说明https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically I was able to complete the prior steps in the package manager console (Visual Studio 2017):我能够在 package 管理器控制台(Visual Studio 2017)中完成前面的步骤:

npm install -g lighthouse
lighthouse https://airhorner.com/
//and
lighthouse https://airhorner.com/ --output=json --output-path=./report/test1.json

However, I do get an initial warning that NPM only supports Node.JS in versions 4 through 8 and recommends a newer version.但是,我确实收到了一个初步警告,即 NPM 仅在版本 4 到 8 中支持 Node.JS,并建议使用更新的版本。 The problem is I am running Node v12 and NPM v5 - both the latest.问题是我正在运行 Node v12 和 NPM v5 - 都是最新的。

When I create a script version like below (app.js)当我创建如下脚本版本时(app.js)

const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const config = {
    extends: 'lighthouse:default',
    settings: {
        emulatedFormFactor: 'desktop',
        onlyCategories: 'performance',
        output: 'json',
        outputPath: './report.json'
    }
};

function launchChromeAndRunLighthouse(url, opts = null, config) {
    return chromeLauncher.launch().then(chrome => {
        opts.port = chrome.port;
        return lighthouse(url, opts, config).then(results => {
            return chrome.kill().then(() => results.lhr);
        });
    });
}

// Usage:
launchChromeAndRunLighthouse('https://airhorner.com/', config).then(results => {
    // Use results!
});

And run the command并运行命令

C:\src\project> node app.js

I get the error - Cannot find module 'lighthouse'我收到错误 - 找不到模块“灯塔”

don't install lighthouse locally use it inside your working dir.不要在本地安装灯塔,在您的工作目录中使用它。 first start by running npm init that will create the package.json file inside the current working dir首先运行npm init启动,它将在当前工作目录中创建 package.json 文件
then npm install --save lighthouse will download it and save it to node_modules now you can use it locally inside your working dir然后npm install --save lighthouse将下载它并将其保存到 node_modules 现在您可以在工作目录中本地使用它

it should look something like this它应该看起来像这样

  • app.js应用程序.js
  • package.json package.json
  • node_modules/节点模块/

then run node app.js然后运行node app.js

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

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