netlify-stripe-subscriptions icon indicating copy to clipboard operation
netlify-stripe-subscriptions copied to clipboard

Help converting fauna.js to be compatible with node-fetch (v3) and ES modules

Open mashdot opened this issue 4 years ago • 6 comments

I have been trying all day to figure out how to fix /functions/utils/fauna.js as it is now failing due to node-fetch (3.0.0) and from what I understand is the new ES modules (ECMAScript modules).

I can't work out how to change the...

const fetch = require('node-fetch');

... into the new "import" format?

I have tried import fetch from 'node-fetch'; but this fails with a SyntaxError: Cannot use import statement outside a module error.

I can't define "type": "module" in package.json or I will get the following error: .eleventy.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.

Any help would be wonderful, thanks!

Original error

Discovered error from the Netlify functions log for identity-signup.js

Uncaught Exception {"errorType":"Error","errorMessage":"Must use import to load ES Module: /var/task/node_modules/node-fetch/src/index.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/node_modules/node-fetch/src/index.js from /var/task/functifigur eons/utils/fauna.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.\nInstead rename index.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/node_modules/node-fetch/package.json.\n","code":"ERR_REQUIRE_ESM","stack":["Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/node_modules/node-fetch/src/index.js","require() of ES modules is not supported.","require() of /var/task/node_modules/node-fetch/src/index.js from /var/task/functions/utils/fauna.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.","Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/node_modules/node-fetch/package.json.",""," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Module.require (internal/modules/cjs/loader.js:887:19)"," at require (internal/modules/cjs/helpers.js:74:18)"," at Object.<anonymous> (/var/task/functions/utils/fauna.js:3:15)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"]}

mashdot avatar Sep 19 '21 19:09 mashdot

A lot of discussion here: https://github.com/node-fetch/node-fetch/issues/1263

mashdot avatar Sep 19 '21 19:09 mashdot

https://github.com/node-fetch/node-fetch/issues/1266

I am going to downgrade for now...

npm install -s node-fetch@2

mashdot avatar Sep 20 '21 10:09 mashdot

I'm running into the same error (and not at all experienced with this, just learning as I go). Did the downgrade work for you?

desidem avatar Nov 16 '21 16:11 desidem

You can replace node-fetch with isomorphic-fetch.

Then in your code use...

const fetch = require('isomorphic-fetch');

mashdot avatar Nov 16 '21 16:11 mashdot

That part works now! It's finding my identity-signup.js file. Thank you! I have a new problem with Stripe. But that's better than a function file not triggering.

desidem avatar Nov 16 '21 19:11 desidem

This worked for me.

  1. In netlify.toml add
[functions]
  node_bundler = "esbuild"
  1. In fauna.js, change
const fetch = require('node-fetch');

to

import fetch from 'node-fetch';

ben519 avatar Jan 14 '22 04:01 ben519