seriousme / fastify-openapi-glue

A plugin for the Fastify webserver to autogenerate a Fastify configuration based on a OpenApi(v2/v3) specification.
MIT License
202 stars 34 forks source link

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module #453

Closed jedaan closed 1 year ago

jedaan commented 1 year ago

Hi , i am using fastify with typescript getting the below error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ....\node_modules\fastify-openapi-glue\index.js [App] require() of ES modules is not supported. [App] require() of....\node_modules\fastify-openapi-glue\index.js from C:\ppf\yettel-auth\dist\app.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.

my tsconfig { "extends": "fastify-tsconfig", "compilerOptions": { "outDir": "dist", "sourceMap": true }, "include": ["src/*/.ts", "src/routes/customers/schema.ts"] }

seriousme commented 1 year ago

Hi,

thanks for asking! It looks to me like the error comes from: C:\ppf\yettel-auth\dist\app.js Are you sure that this file uses import instead of require ?

Kind regards, Hans

github-actions[bot] commented 1 year ago

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'

mhamann commented 1 year ago

I have this same problem. Using typescript, it converts this:

const fastifyOpenApiGlue = await import('fastify-openapi-glue');

into this:

const fastifyOpenApiGlue = await Promise.resolve().then(() => __importStar(require('fastify-openapi-glue')));
seriousme commented 1 year ago

Hi Matt,

The trick seems to be to prevent Typescript from converting back to commonJS. Did you see: https://github.com/seriousme/fastify-openapi-glue/issues/427 ?

If that does not resolve it can you please share your tsconfig.json ?

Kind regards, Hans

mhamann commented 1 year ago

In looking at #427, it seems that part of the solution is setting type: module in package.json. We have a blend of TS and JS files in our package, so converting to a pure module would be a huge lift.

This is our current tsconfig:

{
  "compilerOptions": {
    "target": "es2017",                                  

    "experimentalDecorators": true,                   
    "emitDecoratorMetadata": true,                    

    "module": "commonjs",                                
    "rootDir": "./src",                                  
    "resolveJsonModule": true,                        
    "allowJs": true,                                  
    "sourceMap": true,                                
    "outDir": "./build",                                   
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "skipLibCheck": true                                 
  },
  "include": [
    "./**/*",
    "src/**/*.json"
  ],
  "exclude": [
      "./test/**/*",
      "./build/**/*",
      "./*.js",
      "node_modules"
  ]
}
seriousme commented 1 year ago

Looking at: https://2ality.com/2021/06/typescript-esm-nodejs.html you might want to set:

"module": "ES2020", // (A)
"moduleResolution": "Node", // (B) 
"allowSyntheticDefaultImports": true, // (C)

Hope this helps!