Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main
Hello, im having an issue importing this module when running even the simplest example. After upgrading this package from 2.7.x to latest, I'm getting this error when attempting to import
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/x/node_modules/fastify-openapi-glue/package.json
at new NodeError (node:internal/errors:372:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
at packageExportsResolve (node:internal/modules/esm/resolve:693:7)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/x/dist/index.js:18:43) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
Is it something with the new esm update? I'm using typescript 4.7.4 and im using node 16.
index.ts
import fastify from "fastify";
import cors from "@fastify/cors";
import fastifyOpenapiGlue from "fastify-openapi-glue";
// const fastifyOpenapiGlue = require("fastify-openapi-glue");
const app = fastify();
app.register(cors);
app.register(fastifyOpenapiGlue);
console.log("fastify", fastify);
tsconfig.json
{
"compilerOptions": {
"target": "ES2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "CommonJS" /* Specify what module code is generated. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
start script:
tsc && node index.js
Hi,
thanks for asking !
The challenge is indeed caused by migrating to ESM. I think there are a few options hat might solve this problem:
- Change module format from
commonJStonodenextin tsconfig.json - Add
"moduleResolution": 'nodenext'to tsconfig.json
See: https://www.typescriptlang.org/docs/handbook/esm-node.html And if that does not work, try:
- use:
import { fastifyOpenapiGlue } from "fastify-openapi-glue";
Hope this helps, please let me know if any (or all ;-)) of these options work !
Kind regards, Hans
Hi Hans, thanks for replying!
I changed to
{
"compilerOptions": {
"target": "ES2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "NodeNext" /* Specify what module code is generated. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"moduleResolution": "NodeNext"
}
}
Now i'm getting a red underline under the import, saying:
Cannot find module 'fastify-openapi-glue' or its corresponding type declarations.ts(2307)
If I ignore the ts error and run it, it will yield same error as before :(
Ok, I tried it myself but somehow Typescript keeps generating CommonJS
"use strict";
exports.__esModule = true;
var fastify_1 = require("fastify");
var fastify_openapi_glue_1 = require("fastify-openapi-glue");
// const fastifyOpenapiGlue = require("fastify-openapi-glue");
var app = (0, fastify_1["default"])();
app.register(fastify_openapi_glue_1["default"]);
console.log("fastify", fastify_1["default"]);
Ok, I think I got it working.
Its seems to be a quirck in Typescript :-(
I followed the instructions at: https://2ality.com/2021/06/typescript-esm-nodejs.html and got after compilation in index.js:
import fastify from "fastify";
import fastifyOpenapiGlue from "fastify-openapi-glue";
// const fastifyOpenapiGlue = require("fastify-openapi-glue");
const app = fastify();
app.register(fastifyOpenapiGlue);
console.log("fastify", fastify);
However this only happens if tsconfig contains:
"rootDir": "ts",
"outDir": "dist",
If not, it will fall back to commonJS again !??!
btw: Package.json must be set to "type": "module"
Hope this helps !
Kind regards, Hans
Thanks for investigating this, I really had many issues working with ESM when it comes to TypeScript
I think I got it working using in package.json:
"type": "module"
tsconfig.json
{
"compilerOptions": {
"target": "ES2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "Es2020" /* Specify what module code is generated. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"moduleResolution": "Node",
"outDir": "dist",
"rootDir": "src"
}
}
.js file become this: :)
import fastify from "fastify";
import cors from "@fastify/cors";
import { fastifyOpenapiGlue } from "fastify-openapi-glue";
// const fastifyOpenapiGlue = require("fastify-openapi-glue");
const app = fastify();
app.register(cors);
app.register(fastifyOpenapiGlue);
console.log("fastify", fastify);
Good to read you got a solution !
I presume more people will have issues with this subject as many packages are migrating to ESM. Lets hope the Typescript team documents this a little better ;-)
Kind regards, Hans
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days'
Hello, does anyone here using tsc-esm? The issue still persists when using tsc-esm
Hi,
I haven't used tsc-esm however the README.md mentions:
You should not use this package!
Since TS 4.7 it is now possible to set the moduleResolution field to node16 or nodeNext which enforces the use of the .js extension at the end of the imports.
Previously, Typescript allowed to not add the imported file extension if it was a Typescript or Javascript file. This behavior was opposed to the recent developments of Ecmascript. Browsers, Node and Deno all needed the .js extension.
I first developped tsc-esm as a patch to the tsc compiler that would add the .js extension. But this technique has drawbacks. For example, it cannot deal with Typescript aliases. I strongly encourage to follow the Ecmascript guidelines and the recent Typescript extension, that is to use Typescript@^4.7 and to set the moduleResolution field to node16 or nodeNext in tsconfig.json.
https://www.npmjs.com/package/@digitak/tsc-esm
I hope that answers your question ;-)
Kind regards, Hans
Hi,
I haven't used tsc-esm however the README.md mentions:
You should not use this package!
Since TS 4.7 it is now possible to set the moduleResolution field to node16 or nodeNext which enforces the use of the .js extension at the end of the imports.
Previously, Typescript allowed to not add the imported file extension if it was a Typescript or Javascript file. This behavior was opposed to the recent developments of Ecmascript. Browsers, Node and Deno all needed the .js extension.
I first developped tsc-esm as a patch to the tsc compiler that would add the .js extension. But this technique has drawbacks. For example, it cannot deal with Typescript aliases. I strongly encourage to follow the Ecmascript guidelines and the recent Typescript extension, that is to use Typescript@^4.7 and to set the moduleResolution field to node16 or nodeNext in tsconfig.json.
https://www.npmjs.com/package/@digitak/tsc-esm
I hope that answers your question ;-)
Kind regards, Hans
Thank you, appreciate your answer! In the end, I follow the instruction to add the '.js' extention manually and it works. But it looks so weird tbh.
Good to read you got it working !
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days'