cdimascio / express-openapi-validator

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
MIT License
917 stars 208 forks source link

express-openapi-validator/dist/resolvers.js: require() of ES Module not supported #838

Open bogi158 opened 1 year ago

bogi158 commented 1 year ago

Describe the bug I have generated a server with openapi-express-server but I need to convert it to ES6 as I will be using some libraries that do not support CommonJS anymore so I cannot use require for them. I generated a basic server to test the refactor but I keep getting an error from resolver.js. I have tried with express-openapi-validator v3, v4 and v5.

To Reproduce Generate a server with openapi-express-server Generator and convert the files to ES6 standard.

Actual behavior Server loads up correctly, but when an API is called I get the error:

require() of ES Module /controllers/InfoController.js from /node_modules/express-openapi-validator/dist/resolvers.js not supported.\nInstead change the require of InfoController.js in /node_modules/express-openapi-validator/dist/resolvers.js to a dynamic import() which is available in all CommonJS modules."

Expected behavior openAPI validator should run also with ES6 modules normally

Examples and context This is the Controller that is giving the error, but also all the others controllers will throw an error if used. The error appears not on the server startup but on the first API call to the REST endpoint (in this case info/version)

// InfoController.js
import Controller from './Controller.js';
import service from '../services/InfoService.js';

const version = async (request, response) => {
  await Controller.handleRequest(request, response, service.version);
};

export default {
  version,
};
mahnunchik commented 1 year ago

Related issue https://github.com/cdimascio/express-openapi-validator/issues/660

bogi158 commented 1 year ago

Related issue #660

Thank you. I see that the issue is not really solved, and you still have to change the openapi-validator package code to make it work. Not a very elegant solution. I do not see any PR related to the problem so I guess no one really added this feature on the package and everyone is fixing this by themselves locally.