You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地运行AWS Lambda(Node.js)代码时Puppeteer加载yargs依赖失败求助

本地运行AWS Lambda Node.js Chromium代码时的yargs路径错误解决办法

问题场景

我在AWS Lambda(Node.js)环境中尝试运行Chromium浏览器,使用的代码如下:

import chromium from 'chrome-aws-lambda';

...

const browser = await chromium.puppeteer.launch({
  args: chromium.args,
  defaultViewport: chromium.defaultViewport,
  executablePath: await chromium.executablePath,
  headless: true,
  ignoreHTTPSErrors: true,
});

const page = await browser.newPage();

本地运行时抛出错误:

ERROR: Do not know how to load path: ../../node_modules/@puppeteer/browsers/node_modules/yargs/yargs

错误出现在Puppeteer库的这一行代码:

const yargs_1 = __importDefault(require("yargs/yargs"));

相关tsconfig.json配置:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions":  { 
     
    "jsx": "react-jsx",
    /* Language and Environment */
    "target": "ES2015",
    "lib": [
      "es6",
      "DOM"
    ],

    /* Modules */
    "module": "commonjs" /* Specify what module code is generated. */,
    "resolveJsonModule": true /* Enable importing .json files */,

    /* Module Resolution */
    "baseUrl": "." /* This must be specified if "paths" is. */,

    /* JavaScript Support */
    "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
    "sourceMap": true /* Enables the generation of sourcemap files */,

    /* Emit */
    "declaration": true,
    "downlevelIteration": true,
    "outDir": "dist" /* Specify an output folder for all emitted files. */,

    /* Interop Constraints */
    "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
    "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

    /* Type Checking */
    "strict": true /* Enable all strict type-checking options. */,
    "noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied `any` type.. */,
    "noEmitOnError": true,
    /* Completeness */
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  },
  "include": [
    "./config/**/*",
    "serverless.ts",
    "./src/**/*",
    "./lib/**/*",
    "./middleware/**/*",
    "./__tests__/**/*",
    "res/**/*",
    "**/*.tsx",
    "**/*"
  ]
}

解决办法

  • 清理并重新安装依赖
    删除项目根目录下的node_modules文件夹和package-lock.json(或yarn.lock)文件,重新执行安装命令:

    npm install
    # 或使用yarn
    yarn install
    

    此操作可修复依赖嵌套路径异常,确保yargs包被正确安装在顶层依赖中。

  • 调整TypeScript模块解析配置
    在tsconfig.json的compilerOptions中添加moduleResolution: "node",确保Node.js模块解析规则被正确应用,避免路径解析错误:

    "compilerOptions": {
      // ... 其他原有配置
      "moduleResolution": "node"
    }
    
  • 开发环境替换依赖
    本地开发时直接使用常规puppeteer包替代chrome-aws-lambda,避免Lambda专用依赖带来的路径问题,生产环境再切换回chrome-aws-lambda:

    // 开发环境
    import puppeteer from 'puppeteer';
    // 生产环境
    // import chromium from 'chrome-aws-lambda';
    // const puppeteer = chromium.puppeteer;
    
  • 临时修复导入路径(不推荐长期使用)
    如果上述方法无效,可临时修改Puppeteer相关文件的导入语句,将require("yargs/yargs")改为require("yargs")——yargs的主入口已包含yargs对象,无需单独导入子路径。

内容的提问来源于stack exchange,提问作者luis_azevedo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 05:34:59