GithubHelp home page GithubHelp logo

Comments (12)

seriousme avatar seriousme commented on August 19, 2024 1

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.

jonasgroendahl avatar jonasgroendahl commented on August 19, 2024 1

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.

seriousme avatar seriousme commented on August 19, 2024 1

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.

seriousme avatar seriousme commented on August 19, 2024

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:

  1. Change module format from commonJS to nodenext in tsconfig.json
  2. Add "moduleResolution": 'nodenext' to tsconfig.json

See: https://www.typescriptlang.org/docs/handbook/esm-node.html
And if that does not work, try:

  1. 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.

jonasgroendahl avatar jonasgroendahl commented on August 19, 2024

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:

Screenshot 2022-07-12 at 18 59 06

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.

seriousme avatar seriousme commented on August 19, 2024

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.

github-actions avatar github-actions commented on August 19, 2024

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.

yukha-dw avatar yukha-dw commented on August 19, 2024

Hello, does anyone here using tsc-esm? The issue still persists when using tsc-esm

from fastify-openapi-glue.

seriousme avatar seriousme commented on August 19, 2024

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.

yukha-dw avatar yukha-dw commented on August 19, 2024

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.

seriousme avatar seriousme commented on August 19, 2024

Good to read you got it working !

from fastify-openapi-glue.

github-actions avatar github-actions commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.