3

I use Node.js application and TS 4.8, and I updated package file-type, but now my project compilation fails with error

[1] const _fileType = /#PURE/ _interopRequireWildcard(require("file-type")); [1] ^ [1] [1] Error [ERR_REQUIRE_ESM]: require() of ES Module /home/victor/Desktop/alytics/node_modules/file-type/index.js from /home/victor/Desktop/alytics/dist/routes/index.js not supported. [1] Instead change the require of /home/victor/Desktop/alytics/node_modules/file-type/index.js in /home/victor/Desktop/alytics/dist/routes/index.js to a dynamic import() which is available in all CommonJS modules.

There is my swc config: `

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true,
      "dynamicImport": true
    },
    "target": "es2020",
    "paths": {
      "@routes/*": ["./src/routes/*"],
      "@middlewares/*": ["./src/middlewares/*"]
    },
    "baseUrl": "."
  },
  "module": {
    "type": "commonjs"
  }
}

`

And there is my tsconfig: `

{
  "compilerOptions": {
    "target": "es2020",
    "module": "es2020",
    "allowJs": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "typeRoots": ["./node_modules/@types"],
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["es2020"],
    "baseUrl": ".",
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": "Node",
    "skipLibCheck": true,
    "paths": {
      "@routes/*": ["./src/routes/*"],
      "@middlewares/*": ["./src/middlewares/*"]
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

`

Where I use file-type: `

import KoaRouter from "@koa/router";
import { Context } from "koa";

import { logger } from "@middlewares/index.js";
import * as FileType from "file-type";

const router = new KoaRouter();

router.get("/", logger, (ctx: Context): void => {
  ctx.body = { message: JSON.stringify(FileType) };
});

export default router;

`

As I understand, file-type package using only ESM import, but , after compilation this import convert to require(). I read about this issue in their repository, but this topic was closed as TS now support ESM.

How can I configure my ts.config to exclude this module to covert to require ??

Is there any way to solve this issue only using TS config?

I find the only solution - convert from commonjs - to ESM (change type: module, and add that to my package.json), but I don't want to update all my import adding .js at the end and I dont want to convert all my imports to ESM.

  • Did you find a solution? I'm facing the same issue when trying to add `notion-utils` to my `firebase-functions` project – Martin Dec 29 '22 at 12:34

1 Answers1

0

So I managed to solve it by following the instructions in accepted answer for this question:

Is there a way to combine ES6 imports and commonJS require in TS files?

PLUS, having "moduleResolution": "node" in tsconfig.json.

Not using "moduleResolution": "node" cause errors for file-type and prisma packages.

busaud
  • 98
  • 9