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

如何让TypeScript优先使用Lambda目录下的node_modules?(AWS Amplify+Next.js)

问题描述

我基于TypeScript开发Next.js前端,后端采用AWS Amplify,希望用TypeScript编写Lambda函数。当前项目根目录有Next.js的tsconfig.json,Lambda函数目录也配置了独立的tsconfig.json,功能可正常运行,但TypeScript会优先查找根目录的node_modules,导致必须将Lambda依赖安装到前端项目中,我希望避免这种情况。

文件结构

📦appchen
 ┣📦amplify
 ┃ ┗ 📂backend
 ┃   ┗ 📂function
 ┃     ┗ 📂appchenAuthPostConfirmation
 ┃       ┣ 📂src
 ┃       ┃ ┣ 📂node_modules 
 ┃       ┃ ┣ 📜index.ts
 ┃       ┃ ┗ 📜package.json     
 ┃       ┣ 📜appchenAuthPostConfirmation-cloudformation-template.json       
 ┃       ┗ 📜tsconfig.json
 ┣ 📂public
 ┣ 📂src
 ┃ ┣ 📂app
 ┃ ┃ ┣ 📜layout.tsx
 ┃ ┃ ┗ 📜page.tsx
 ┃ ┗ 📜aws-exports.js
 ┣ 📜package.json
 ┗ 📜tsconfig.json

根目录tsconfig.json配置

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules, amplify"]
}

Lambda函数tsconfig.json配置

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "lib": ["dom", "esnext"],
    "module": "commonjs",
    "moduleResolution": "node",
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "outDir": "./src",
    "baseUrl": "./",
    "rootDir": "./src",
    "paths": {
      "src": ["./src"]
    }
  },
  "include": ["./src"],
  "exclude": ["src/node_modules", "src/**/*.spec.ts"]
}

我尝试过官方文档提到的lib目录默认配置,但问题依旧。请问如何强制TypeScript使用Lambda函数src目录下的node_modules而非根目录的?嵌套tsconfig.json是否支持这种配置?


解决方案

1. 修复根目录tsconfig.json的exclude语法错误

根目录tsconfig.json中exclude字段的写法有误,"node_modules, amplify"是单个字符串,应该拆分为两个独立元素,确保TypeScript完全排除Amplify函数目录的编译检查:

"exclude": ["node_modules", "amplify"]

这一步能从根源上避免前端TypeScript解析Lambda目录的依赖文件。

2. 配置Lambda的tsconfig.json强制使用本地依赖

在Lambda的tsconfig.json中调整编译选项,明确指定依赖查找路径,优先使用本地node_modules:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "lib": ["ESNext"],
    "module": "commonjs",
    "moduleResolution": "node16",
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "rootDir": "./src",
    "typeRoots": ["./src/node_modules/@types"],
    "isolatedModules": true
  },
  "include": ["./src/**/*"],
  "exclude": ["./src/node_modules"]
}
  • 将outDir改为./dist,避免编译产物和源码混在src目录
  • 启用node16模块解析模式,更严格地遵循Node.js的依赖查找规则
  • 用typeRoots指定类型定义仅从本地node_modules读取

3. 给Lambda配置独立构建命令

在Lambda目录的package.json中添加专属的编译命令,确保构建时使用本地tsconfig.json:

{
  "scripts": {
    "build": "tsc --project ../tsconfig.json"
  }
}

同时在Amplify函数配置中指定使用该命令,确保Amplify构建Lambda时完全采用本地配置,不继承根目录设置。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 00:48:13