"ERR_REQUIRE_ESM" error in "@forestadmin/datasource-customizer" caused by "file-type" package and "node-fetch"

Feature(s) impacted

The error message I provided suggests that the @forestadmin/datasource-customizer package is trying to import an ES module using the require() method. However, the require() method does not support ES modules. This issue seems to be caused by the file-type package, which is an ES module, and it’s being imported by @forestadmin/datasource-customizer. How do I fix this issue?

Observed behavior

After I try to run my Nest.js server, it immidiately stops with an error.

Expected behavior

Nest.js should run without a problem.

Failure Logs

G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\decorators\binary\collection.js:7
const file_type_1 = __importDefault(require("file-type"));
                                    ^
Error [ERR_REQUIRE_ESM]: require() of ES Module G:\Dokumenty\Projekty\pla-manager\backend\node_modules\file-type\index.js from G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\decorators\binary\collection.js not supported.
Instead change the require of index.js in G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\decorators\binary\collection.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\decorators\binary\collection.js:7:37)
    at Object.<anonymous> (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\decorators\decorators-stack.js:7:38)
    at Object.<anonymous> (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\datasource-customizer.js:8:44)
    at Object.<anonymous> (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\datasource-customizer\dist\index.js:23:31)
    at Object.<anonymous> (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@forestadmin\agent\dist\index.js:7:33)
    at Object.<anonymous> (G:\Dokumenty\Projekty\pla-manager\backend\dist\src\main.js:11:17)

Context

  • Project name: pla-manager
  • Team name: personal space
  • Environment name: node.js
  • Agent (forest package) name & version: @forestadmin/agent^1.8.13
  • Database type: typeorm mysql
  • Recent changes made on your end if any: It’s actually weird because there were no errors until I restarted app so I don’t know at which point it stopped working. Recent change was importing some config related module that is importing yaml config.

Additional code

package.json

{
  "name": "pla-manager",
  "version": "0.0.0",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build public_nodejs",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start --config ./nest-cli.json",
    "start:dev": "set NODE_ENV=development&&nest start --watch --config ./nest-cli.json",
    "start:debug": "nest start --debug --watch --config ./nest-cli.json",
    "start:prod": "set NODE_ENV=production&&node dist/main.js --config ./nest-cli.json",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "depcheck": "npx depcheck --detailed"
  },
  "dependencies": {
    "@adminjs/express": "5.1.0",
    "@adminjs/nestjs": "5.1.1",
    "@adminjs/typeorm": "4.0.0",
    "@forestadmin/agent": "^1.8.13",
    "@forestadmin/datasource-sql": "^1.5.1",
    "@nestjs/common": "^9.4.0",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.4.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/serve-static": "^3.0.1",
    "@nestjs/testing": "^9.4.0",
    "@nestjs/typeorm": "^9.0.1",
    "@tiptap/pm": "^2.0.3",
    "adminjs": "6.8.7",
    "class-validator": "^0.14.0",
    "connect-typeorm": "^2.0.0",
    "discord.js": "^14.9.0",
    "express": "^4.18.2",
    "express-formidable": "^1.2.0",
    "express-session": "^1.17.3",
    "js-yaml": "^4.1.0",
    "mysql": "^2.18.1",
    "mysql2": "^3.2.4",
    "necord": "^5.7.3",
    "nest-puppeteer": "^1.1.1",
    "nest-winston": "^1.9.1",
    "node-fetch": "^2.6.6",
    "passport": "^0.6.0",
    "passport-discord": "^0.1.4",
    "puppeteer": "^20.1.0",
    "react": "^18.2.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.0.0",
    "scheduler": "^0.23.0",
    "tslib": "^2.5.0",
    "typeorm": "^0.3.15",
    "winston": "^3.8.2"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@types/express": "^4.17.17",
    "@types/express-session": "^1.17.7",
    "@types/jest": "28.1.8",
    "@types/js-yaml": "^4.0.5",
    "@types/node": "^18.16.3",
    "@types/passport-jwt": "^3.0.8",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "depcheck": "^1.4.3",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.8",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "node16",
    "module": "commonjs",
    "target": "esnext",
    "jsx": "react",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "typeRoots": ["./node_modules/@types", "src/@types"],
    "types": ["puppeteer", "jest", "node", "express"]
  }
}

Hi @dudematthew

We do depend on the file-type package which is an ESM package, but the version we depend on is an older one (v16), which should be OK to use with require.

The dependency was actually introduced very recently when I added the support for binary column types for SQL databases and Mongo.

Our package.json

Can you check what version of file-type you have on your node_modules?
Should not be 18.x.x.

Did you alter your npm/yarn lock file in any way?

I use Yarn. I don’t have file-type as package.json dependency as it is a sub-dependency of one of Your modules.

Here’s my lock file, that - according to Your advices - indeed has a v18 of file-type. It is a dependency provided by @discordjs/rest package

yarn.zip (128.0 KB)

Could you analize my yarn.lock and explain what I should do to resolve this issue? I’ll try to learn to be able to do it on my own in the feature :slight_smile:

I’ve checked and added below lines to the package.json:

"resolutions": {
    "@discordjs/rest/file-type": "16.5.4",
    "file-type": "16.5.4"
  },

and then used yarn command - that resolved the issue.
Thank You for help @anon39940173!

@anon39940173 I was forced to migrate to npm and now I can’t use resolutions. What should I do?

Hello,
Did you try to to use overrides ?

I did and it worked. Thank You for your help.

1 Like