Slicemachine not working anymore when having node script with custom fetch implementation

Inside my NextJS project I have a node script in scripts/build-search.mjs which fetches all my documents and indexes them for algolia.

Because that's outside of my NextJS environment I need a polyfill for fetch.

I Implemented it in the suggested way by using node-fetch.

import fetch from 'node-fetch'

...

const client = prismic.createClient(process.env.PRISMIC_API_ENDPOINT, {
    ...config,
    accessToken,
    routes: routeResolver.routes,
    fetch,
  })

Whenever I try to run the slice-machine now, I get this error:

[slice-machine] /Users/marcokuehbauch/development/dieda-website/node_modules/slice-machine-ui/build/server/src/index.js:141
undefined
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/marcokuehbauch/development/dieda-website/node_modules/node-fetch/src/index.js from /Users/marcokuehbauch/development/dieda-website/node_modules/slice-machine-ui/build/server/src/index.js not supported.
Instead change the require of /Users/marcokuehbauch/development/dieda-website/node_modules/node-fetch/src/index.js in /Users/marcokuehbauch/development/dieda-website/node_modules/slice-machine-ui/build/server/src/index.js to a dynamic import() which is available in all CommonJS modules.
at Object.newLoader [as .js] (/Users/marcokuehbauch/development/dieda-website/node_modules/pirates/lib/index.js:141:7)
at Object. (/Users/marcokuehbauch/development/dieda-website/node_modules/slice-machine-ui/build/server/src/index.js:16:38) {
code: 'ERR_REQUIRE_ESM'

When I remove my node-fetch implementation completely and run npm run slicemachine again, the slicemachine works again.

Why are these related and do you have any idea, how I solve this?

Hi @marco3,

Thanks for reaching out,

In fact the error is telling you to use either Import or require but not both, can you try to replace import fetch by require?

Hey @Fares

Unfortunately this didn't change the error. I also tried it with v2 of node-fetch and the async import as described in the docs node-fetch - npm.

I replaced node-fetch by cross-fetch and now slicemachine is working again. I have no clue why node-fetch and slicemachine don't like each other.

Well, I've seen a similar issue before, and it turns out that it was related to the node-fetch version, please check this topic reply.

Tracking this issue. While it is likely not a "huge" issue, using node-fetch@2.6.1 from September of 2020 seems like a potential issue and one that should be addressed by the Slice-Machine team.

Hi @springtreedigital

Thanks for letting us know; as you know, Slice Machine is an open source project, so feel free to raise an issue on the Project page as well as PR if you would like to contribute.

On my side, I will check with Slice Machine team if there is a solution for this, but before all, can you please confirm that using the latest version of node-fetch still doesn't work?

Sure thing. Using latest version 3 of node-fetch. It's in their docs that if this is an issue, then just use the latest of version 2. I didn't know this at the time.

Thanks for your reply,
I have reported this to our dev team and I will let you know in case of any updates.

Any update for support of v3 ?

Hey @cptflammin, I suppose you're referring to Nuxt 3. Here are the updates we have for the moment:

Hey @Pau, no I am referring to the possibility to use node-fetch v3. Current workaround requires to downgrade to node-fetch v2

I believe this is the same error: `ERR_REQUIRE_ESM` error on `start-slicemachine` due to incorrect `node-fetch` version · Issue #467 · prismicio/slice-machine · GitHub
It hasn’t been addressed yet. It'll be good if you can share your use case in the GitHub issue as well, so the dev team can see it.

@Pau Seems solved with latest helpers update Type Error when building package that uses @prismicio/helpers - #10 by cptflammin

1 Like