Node.js使用mime/file-type模块触发ERR_REQUIRE_ESM错误求助
错误原因解析:TypeScript项目中使用
mime/file-type模块触发ERR_REQUIRE_ESM 问题重现
在TypeScript项目中尝试用mime模块识别文件类型,编写代码如下:
import mime from 'mime'; // import { mime } from 'mime'; // 也尝试过该写法 const determineFileType = async (filePath) => { const type = mime.getType(filePath); if (type) { logger.info(`File type is: ${type.mime} and the extension is: ${type.ext}`); } else { logger.info(`Could not determine the file type for ${filePath}`); } return type; };
执行后触发错误:
2024-Feb-18 09:11:29:1129 +0ms ip-172-31-47-89 pid:586768 error: uncaughtException: require() of ES Module /home/ubuntu/my-app/node_modules/mime/dist/src/index.js from /home/ubuntu/my-app/src/utils/decompress.ts not supported. Instead change the require of index.js in /home/ubuntu/my-app/src/utils/decompress.ts to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module /home/ubuntu/my-app/node_modules/mime/dist/src/index.js from /home/ubuntu/my-app/src/utils/decompress.ts not supported. Instead change the require of index.js in /home/ubuntu/my-app/src/utils/decompress.ts to a dynamic import() which is available in all CommonJS modules. at require.extensions.<computed> [as .js] (/home/ubuntu/my-app/node_modules/ts-node/dist/index.js:851:20) at Object.<anonymous> (/home/ubuntu/my-app/src/utils/decompress.ts:19:32) at m._compile (/home/ubuntu/my-app/node_modules/ts-node/dist/index.js:857:29) (node:586768) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) [nodemon] app crashed - waiting for file changes before starting...
随后改用file-type模块:
import { fileTypeFromFile } from 'file-type'; ... const type = await fileTypeFromFile(filePath); ...
触发类似错误:
2024-Feb-18 09:14:12:1412 +0ms ip-172-31-47-89 pid:587367 error: uncaughtException: require() of ES Module /home/ubuntu/my-app/node_modules/file-type/index.js from /home/ubuntu/my-app/src/utils/decompress.ts not supported. Instead change the require of index.js in /home/ubuntu/my-app/src/utils/decompress.ts to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module /home/ubuntu/my-app/node_modules/file-type/index.js from /home/ubuntu/my-app/src/utils/decompress.ts not supported. Instead change the require of index.js in /home/ubuntu/my-app/src/utils/decompress.ts to a dynamic import() which is available in all CommonJS modules. at require.extensions.<computed> [as .js] (/home/ubuntu/my-app/node_modules/ts-node/dist/index.js:851:20) at Object.<anonymous> (/home/ubuntu/my-app/src/utils/decompress.ts:19:21) at m._compile (/home/ubuntu/my-app/node_modules/ts-node/dist/index.js:857:29) (node:587367) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created)
错误核心原因
这个错误的本质是模块系统不兼容:
mime和file-type的最新版本都是纯ES模块(ESM),这类模块在自身的package.json中会声明"type": "module",Node.js会强制以ESM规则加载它们。- 你的TypeScript项目当前是以**CommonJS(CJS)**模式运行的:
- 要么你的项目
package.json中没有设置"type": "module",Node.js默认将编译后的.js文件当作CJS处理; - 要么你的
tsconfig.json中compilerOptions.module设置为CommonJS(默认值),导致ts-node将TypeScript代码编译成CJS格式的代码。
- 要么你的项目
- Node.js禁止在CJS模块中用
require()加载ESM模块,而ts-node编译后的CJS代码尝试用require()加载这两个ESM依赖,直接触发了ERR_REQUIRE_ESM错误。
内容的提问来源于stack exchange,提问作者Kid_Learning_C
相关产品推荐
相关产品推荐

