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

Azure Functions(TypeScript)导入自定义模块遇ERR_MODULE_NOT_FOUND错误

解决Azure Functions TypeScript导入自定义模块的模块找不到错误

问题根源

你的项目在package.json中设置了"type": "module",启用了ES模块模式。Node.js在ES模块模式下要求所有相对导入必须指定完整的文件扩展名(比如.js),但TypeScript默认编译后的导入语句不会自动添加.js扩展名,导致运行时找不到对应的模块文件。

解决方案

1. 调整TypeScript编译配置

修改tsconfig.json的compilerOptions,适配ES模块的导入规则:

{
  "compilerOptions": {
    "module": "NodeNext",
    "target": "ESNext",
    "moduleResolution": "NodeNext",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noImplicitAny": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true
  },
  "include": ["./**/*.ts"],
  "exclude": ["node_modules", "dist"]
}
  • module: "NodeNext"和moduleResolution: "NodeNext"会让TypeScript根据项目的ES模块配置,自动生成带.js扩展名的导入语句,匹配Node.js的运行要求。

2. 重新编译并验证

执行npm run build重新编译项目,检查dist/src/functions/createQuoteHttp.js中的导入语句,应该自动变为:

import { RequestProcessor } from '../processors/RequestProcessor.js';

3. 启动服务测试

执行npm start重启Azure Functions,此时自定义模块的导入即可正常工作,不会再触发ERR_MODULE_NOT_FOUND错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 18:15:08