1. tsconfig.json 修改选项 "module": "esnext"
  2. package.json 修改选项 "type": "module"
  3. 将 serverless 函数中的 import foo from './foo' 修改为 import foo from './foo.js'

原理:ESM 规范要求导入模块时使用的必须是完整文件名,也就是说需要带上 .js/.mjs 扩展名。TypeScript 编译器兼容该规范,导本地模块的时候加一个 .js 扩展名就 OK 了


Set VERCEL_CLI_VERSION environment variable to the previous CLI verison [email protected].

Option 2

We are locking this thread now to avoid confusion. As a next step:

We have an update from our engineer that may fix your issue and allow you to remove the CLI variable:
tsconfig.json with "module": "esnext" (or "module": "nodenext") must also have package.json with "type": "module".

If you run into a problem, it will likely be at runtime and look something like:

ReferenceError: require is not defined in ES module scope, you can use import instead

If you want to continue using CommonJS instead of ES Modules, the solution would be to update your tsconfig.json file with "module": "commonjs".

Also note that TypeScript will allow you to mix CJS and ESM, however Node.js will not. So you cannot use require() and import() in the same file, and you cannot use export default and module.exports in the same file.

If you are still facing issue with above configuration, please reach out to Vercel Support with following information:

  • contents of package.json
  • contents tsconfig.json
  • the runtime error message (we can find this if you provide deployment url)
  • the contents of the file that failed (the file will be mentioned in the error message)

If you have set any Environment variable to use previous version of Vercel CLI, please ensure you remove it to get future updates of our Vercel builder. Thank you!

Refer: https://github.com/orgs/vercel/discussions/1225