sindresorhus/file-type

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

Closed this issue · 3 comments

I'm doing an upgrade from ^16.5.3 to ^19.4.1
(because ^16.5.3 stopped working on Node v22 for some reason, fromBuffer always gives undefined for file type...)

the current setup is:
Node: v22.6.0 (same outcome on v20.17.0 as well)
NextJS
module resolution CommonJS
tsconfig: "module": "CommonJS" and "target": "ES2021"

Im getting the following error:

node:internal/modules/cjs/loader:646
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/bojanche/.../node_modules/file-type/package.json
    at exportsNotFound (node:internal/modules/esm/resolve:296:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:586:13)
    at resolveExports (node:internal/modules/cjs/loader:639:36)
    at Module._findPath (node:internal/modules/cjs/loader:747:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1234:27)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at Hook._require.Module.require (/Users/bojanche/.../node_modules/require-in-the-middle/index.js:167:34) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v22.6.0

any idea what is causing this?

From the readme:

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

That's what I ended up with:

export const FileType = {} as typeof import('file-type');

(async () => {
    // biome-ignore lint/security/noGlobalEval: 🤷‍♂️
    Object.assign(FileType, await eval('import("file-type")'));
})();

Just don't use it immediately after importing FileType. However it's safe to use in some async code (e.g., request or event handler).

Better to put the loader in pure JavaScript: like this: https://github.com/Borewit/music-metadata/blob/v10.6.0/lib/default.cjs

Similar rejected solution in file-type: #665