netlify-stripe-subscriptions
netlify-stripe-subscriptions copied to clipboard
Help converting fauna.js to be compatible with node-fetch (v3) and ES modules
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)"]}
A lot of discussion here: https://github.com/node-fetch/node-fetch/issues/1263
https://github.com/node-fetch/node-fetch/issues/1266
I am going to downgrade for now...
npm install -s node-fetch@2
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?
You can replace node-fetch with isomorphic-fetch.
Then in your code use...
const fetch = require('isomorphic-fetch');
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.
- In
netlify.tomladd
[functions]
node_bundler = "esbuild"
- In
fauna.js, change
const fetch = require('node-fetch');
to
import fetch from 'node-fetch';