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.