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

node-fetch v3 is an ESM-only and throw error ERR_REQUIRE_ESM #226

Open
cesco69 opened this issue Jul 26, 2024 · 1 comment
Open

node-fetch v3 is an ESM-only and throw error ERR_REQUIRE_ESM #226

cesco69 opened this issue Jul 26, 2024 · 1 comment

Comments

@cesco69
Copy link
Contributor

cesco69 commented Jul 26, 2024

Hi, I'm on Node.js 20 with typescript, when try to use geoserver-node-client I see this error into the console

Error [ERR_REQUIRE_ESM]: require() of ES Module \[email protected]\node_modules\node-fetch\src\index.js from \[email protected]\node_modules\geoserver-node-client\dist\src\layer.js not supported.
Instead change the require of index.js in \[email protected]\node_modules\geoserver-node-client\dist\src\layer.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (\[email protected]\node_modules\geoserver-node-client\dist\src\layer.js:12:41) {
  code: 'ERR_REQUIRE_ESM'
}

From the node-fetch package readme:

node-fetch is an ESM-only module - you are not able to import it with require. We recommend you stay on v2 which is built with CommonJS unless you use ESM yourself. We will continue to publish critical bug fixes for it.

If you want to require it, then downgrade to v2.

The other option you have is to use async import('node-fetch'), eg.:

  /**
   * Get the GeoServer version.
   *
   * @throws Error if request fails
   *
   * @returns {Object} The version of GeoServer
   */
  async getVersion () {
    const url = this.url + 'about/version.json';
    const response = await (await import('node-fetch')).default(url, {
      credentials: 'include',
      method: 'GET',
      headers: {
        Authorization: this.auth
      }
    });

    if (!response.ok) {
      const geoServerResponse = await getGeoServerResponseText(response);
      throw new GeoServerResponseError(null, geoServerResponse);
    }
    return response.json();
  }

or use CommonJS https://github.com/node-fetch/node-fetch?tab=readme-ov-file#commonjs eg.:

const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

The workaround for use latest geoserver-node-client is install old node-fetch 2.6.8:

@cesco69 cesco69 changed the title ERR_REQUIRE_ESM node-fetch error ERR_REQUIRE_ESM Jul 26, 2024
@cesco69 cesco69 changed the title node-fetch error ERR_REQUIRE_ESM node-fetch v3 is an ESM-only and throw error ERR_REQUIRE_ESM Jul 26, 2024
@chrismayer
Copy link
Collaborator

Hi @cesco69 , thanks for your issue, your analysis and your workarounds 👍 Do you think this is only TypeScript related? Not really, or what do you think?

I have to dig a bit to fully understand whats going on and think what could be the best solution for this. I'll come back to this...

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

2 participants