RT
最近我写的项目也受到了影响,翻了个把小时的讨论版,找到了解决方案
TL;DR
- 在
tsconfig.json
修改选项"module": "esnext"
- 在
package.json
修改选项"type": "module"
- 将 serverless 函数中的
import foo from './foo'
修改为import foo from './foo.js'
原理:ESM 规范要求导入模块时使用的必须是完整文件名,也就是说需要带上 .js/.mjs
扩展名。TypeScript 编译器兼容该规范,导本地模块的时候加一个 .js
扩展名就 OK 了
Option 1 (NOT RECOMMEND)
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:
A 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:
1 | 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!