Trying to import fastify-openapi-glue
in my fastify project but getting error ERR_REQUIRE_ESM
and I'm using typescript in the application.
Code looks like below
routes.ts file
/* eslint-disable node/no-unsupported-features/es-syntax */
import { FastifyInstance } from 'fastify';
// import openapiGlue from 'fastify-openapi-glue';
// const openapiGlue = await import("fastify-openapi-glue");
import { Service } from '../controllers/service';
async function loadOpenapiGlue() {
const openapiGlueModule = await import('fastify-openapi-glue');
return openapiGlueModule.default;
}
const options = {
specification: './openapi.yaml',
service: new Service()
};
export default async (app: FastifyInstance): Promise<void> => {
const openapiGlue = await loadOpenapiGlue();
app.register(openapiGlue, options);
};
tsconfig.json file
{
"ts-node": {
"require": ["tsconfig-paths/register"]
},
"compilerOptions": {
"alwaysStrict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"lib": ["ES2020"],
"module": "commonjs",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": "./src",
"rootDirs": ["src"],
"outDir": "dist",
"plugins": [],
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"target": "ES2020",
"types": ["node", "jest"],
"paths": {
"@/*": ["*"]
}
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
Note:
After updating the module under tsconfig.json as mentioned in other stackoverflow answer as "module": "ES2022"
, I'm getting error like SyntaxError: Cannot use import statement outside a module