sindresorhus/file-type

file-type ERR_PACKAGE_PATH_NOT_EXPORTED typescript

Closed this issue · 4 comments

Seems like file-type is not working well or to be exact can't be found in a Typescript environment. My config is bad is working pretty well with the other libraries in my initial project so i guess file-type is the problem here.
I isolated it in a separate project to see if the same problem occur again and it does.
This is the error I get:

[Typescript]
[Node] Error: No "exports" main defined in node_modules/file-type/package.json
[Node]     at exportsNotFound (node:internal/modules/esm/resolve:302:10)
[Node]     at packageExportsResolve (node:internal/modules/esm/resolve:592:13)
[Node]     at resolveExports (node:internal/modules/cjs/loader:596:36)
[Node]     at Function.Module._findPath (node:internal/modules/cjs/loader:673:31)
[Node]     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1135:27)
[Node]     at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/test/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
[Node]     at Function.Module._load (node:internal/modules/cjs/loader:990:27)
[Node]     at Module.require (node:internal/modules/cjs/loader:1237:19)
[Node]     at require (node:internal/modules/helpers:176:18)
[Node]     at/test/src/server.ts:2:12 {
[Node]   code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
[Node] }
[Node] ts-node src/server.ts exited with code 1

The steps to reproduce are simple. Here is my tree:

├── dist
│   └── server.js
├── package.json
├── package-lock.json
├── src
│   └── server.ts
├── test.jpg
└── tsconfig.json

in src/server.ts:

(async ()=>{
   const fileType = await import("file-type")
   const type = await fileType.fileTypeFromFile("test.jpg")
   console.log(type)
})()

package.json:

{
    "name": "test",
    "version": "1.0.0",
    "main": "server.ts",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "concurrently -k -n \"Typescript,Node\" -c \"blue,green\" \"tsc --watch\" \"ts-node src/server.ts\""
    },
    "dependencies": {
        "@types/file-type": "^10.9.1",
        "file-type": "^19.5.0"
    },
    "devDependencies": {
        "@types/node": "^20.12.12",
        "concurrently": "^8.2.2",
        "nodemon": "^3.1.0",
        "ts-node": "^10.9.2",
        "typescript": "^5.4.5"
    }
}

my basic tsconfig.json for this test project:

{
    "compilerOptions": {
        "outDir": "dist",
        "module": "CommonJS",
        "target": "ESNext",
        "strict": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "skipLibCheck": true
    },
    "include": ["src/**/*.ts"]
}

Note: I ran this same code in pure Javascript, no Typescript support and it work perfectly fine so maybe my config is at fault but i'm not good enough to see it :)

"module": "CommonJS",

This is your problem.


From the readme:

This package is an ESM package. Your project needs to be ESM too. Read more.

Then the problem lies in TS. There is a probability it's converting the import() statement to a require()

Then the problem lies in TS. There is a probability it's converting the import() statement to a require()

Correct, for an option to resolve that see: load-esm