sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module \node_modules\got\dist\source\index.js from ... not supported. #1968

Closed Nelie-Taylor closed 2 years ago

Nelie-Taylor commented 2 years ago

Describe the bug

Actual behavior

image

Code to reproduce

import { NextFunction, Request, Response } from 'express';
import got from 'got';
import { RecaptchaSecert } from '@config/FOther';
import { IRecaptcha } from '@interfaces/third-party/IRecaptcha';

export default async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  const GRecaptcha = req.headers['captcha'];
  if (process.env.NODE_ENV !== 'production' && GRecaptcha === 'NGHI') {
    return next();
  }
  try {
    if (GRecaptcha && GRecaptcha !== '') {
      const recaptcha: IRecaptcha = await got.get(
        `https://www.google.com/recaptcha/api/siteverify?secret=${RecaptchaSecert}&response=${GRecaptcha}&remoteip=${req._ip}`
      )
        .json<IRecaptcha>();
      if (recaptcha.success) {
        return next();
      }
    }
  } catch (error) {
    return next({
      code: -10,
      mess: error
    });
  }
  next({
    code: -2,
    mess: 'Cannot verify recaptcha'
  });
};

Checklist

Thank you for watching.

Nelie-Taylor commented 2 years ago

I just try "got": "~11.8.3", -> It work for me.

szmarczak commented 2 years ago

image

=> await import('got')