Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with ESM #12

Open
machineghost opened this issue Jun 13, 2023 · 3 comments
Open

Doesn't work with ESM #12

machineghost opened this issue Jun 13, 2023 · 3 comments

Comments

@machineghost
Copy link

machineghost commented Jun 13, 2023

I tried to use this library, but apparently, it can't handle ES Modules (ie. "type": "module" in package.json),.

When I tried to run it, it immediately failed after trying to require one of my (ESM) API endpoints (feedback.js).

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/me/project/next/pages/api/feedback.js from /home/me/project/next/node_modules/nextjs-http-supertest/index.js not supported.
Instead change the require of feedback.js in /home/me/project/next/node_modules/nextjs-http-supertest/index.js to a dynamic import() which is available in all CommonJS modules.
@mtbrault
Copy link
Owner

When I read the error, the problem is that you are using const package = require('package') instead of import package from 'package' doesn't it ?

@machineghost
Copy link
Author

Sorry, I should have included the stack trace to make things clearer ... and now I've stopped using Supertest and can't easily repro the error.

However, the core problem was that:

 const package = require('package')

wasn't coming from my code ... it was coming from Supertest code. My code is using ESM, and only has imports.

@developerasun
Copy link

Sorry, I should have included the stack trace to make things clearer ... and now I've stopped using Supertest and can't easily repro the error.

However, the core problem was that:

 const package = require('package')

wasn't coming from my code ... it was coming from Supertest code. My code is using ESM, and only has imports.

if your problem comes from using ESM in CJS project like error message suggests, try dynamic imports like below.

async function _load() {
  const server = await import('nextjs-http-supertest')

  // your codes
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants