nodejs'文件类型'和'mime':不支持ES模块的require();将 index.js 更改为 CommonJS 模块中可用的动态 import()

问题描述 投票:0回答:1

import mime from 'mime';
// import { mime } from 'mime'; // this is also tried

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...

我也尝试过这段代码:

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)

这里真的很困惑。

这个错误是什么?我找不到线索......

node.js import es6-modules file-type
1个回答
0
投票

尝试使用 mime 版本 2.5.2。反而。我在使用 mime 4.0.4 时遇到了同样的错误,之后错误就消失了。

© www.soinside.com 2019 - 2024. All rights reserved.