Comments (12)
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
from fastify-openapi-glue.
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);
from fastify-openapi-glue.
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
from fastify-openapi-glue.
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
commonJS
tonodenext
in 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
from fastify-openapi-glue.
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 :(
from fastify-openapi-glue.
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"]);
from fastify-openapi-glue.
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'
from fastify-openapi-glue.
Hello, does anyone here using tsc-esm? The issue still persists when using tsc-esm
from fastify-openapi-glue.
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
from fastify-openapi-glue.
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.
from fastify-openapi-glue.
Good to read you got it working !
from fastify-openapi-glue.
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'
from fastify-openapi-glue.
Related Issues (20)
- Error in Response Validation Message HOT 9
- Middleware HOT 3
- [email protected] deprecation notice (FSTDEP012) "Request#context will be removed in `fastify@5` HOT 4
- No validation error when non `application/json` response provided by API HOT 2
- Return route options from operationResolver HOT 2
- [Discussion] [Request] Rename `service` => `handlers` in options HOT 4
- Suggestion about the TS imports errors HOT 7
- using "example" in swagger api HOT 7
- PARAMETERS method is not supported: when using cross-method parameters HOT 3
- Schema validation HOT 2
- Presence of cookie not being validated HOT 5
- and connected security requirements not possible. HOT 7
- Multiple Security Handlers Error Handling HOT 4
- "Error: 'specification' parameter must contain a valid version" when trying to reference schema from another file HOT 6
- FEATURE REQUEST: Mark some routes as unrouted/static (no operationId logic) HOT 3
- References getting stripped from response HOT 2
- TypeScript template for fastify-openapi-glue cli? HOT 2
- `opts` ignored in default template? HOT 6
- Compression doesn't work HOT 1
- PR for no-dependency cookie support? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-openapi-glue.