首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NPM和NodeJS兼容性: NodeJS工作于PM提示,而不是脚本

NPM和NodeJS兼容性: NodeJS工作于PM提示,而不是脚本
EN

Stack Overflow用户
提问于 2019-10-28 19:00:43
回答 1查看 523关注 0票数 2

我正在尝试在Node.JS中运行一个灯塔脚本(我是新手)。我遵循了这里的原始指令,https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically。我能够在包管理器控制台(Visual 2017)中完成前面的步骤:

代码语言:javascript
复制
npm install -g lighthouse
lighthouse https://airhorner.com/
//and
lighthouse https://airhorner.com/ --output=json --output-path=./report/test1.json

但是,我确实收到了一个初步警告,即NPM只支持版本4到8的Node.JS,并推荐更新版本。问题是我正在运行Node v12和NPM v5 --这两个都是最新的。

当我创建如下脚本版本时(app.js)

代码语言:javascript
复制
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!
});

并运行命令

代码语言:javascript
复制
C:\src\project> node app.js

我发现错误-找不到“灯塔”模块

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-28 19:32:20

不要在本地安装灯塔,在工作区域内使用它。首先,运行npm init,在当前工作dir中创建package.json文件

然后npm install --save lighthouse将下载它并将其保存到node_modules,现在您可以在工作dir中本地使用它了。

应该是这样的

  • app.js
  • package.json
  • node_modules/

然后运行node app.js

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58596892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档