Unable to import `node-fetch` in service

I’ve got code like the following in one of my api service files…

import { Connection } from 'jsforce'
import fetch from 'node-fetch'

async function getToken() {
  const url = `https://example.com/services/oauth2/token` security token.
  const password = `blarg`

  const response = await fetch(url)
  const json = await response.json()
  const accessToken = json.access_token

  return accessToken
}

I’ve also confirmed that I’ve got node-fetch installed in the package.json in my /api directory, however whenever I try to access the service I get the following message about the node-fetch import.

yarn run v1.22.10
$ /workspaces/force-redwood-test/node_modules/.bin/rw dev
$ /workspaces/force-redwood-test/node_modules/.bin/cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn nodemon --watch /workspaces/force-redwood-test/redwood.toml --exec 'yarn rw-api-server-watch'
$ /workspaces/force-redwood-test/node_modules/.bin/cross-env NODE_ENV=development RWJS_WATCH_NODE_MODULES= webpack serve --config /workspaces/force-redwood-test/node_modules/@redwoodjs/core/config/webpack.development.js
$ /workspaces/force-redwood-test/node_modules/.bin/rw-gen-watch
$ /workspaces/force-redwood-test/node_modules/.bin/nodemon --watch /workspaces/force-redwood-test/redwood.toml --exec 'yarn rw-api-server-watch'
api | [nodemon] 2.0.15
api | [nodemon] to restart at any time, enter `rs`
api | [nodemon] watching path(s): redwood.toml
api | [nodemon] watching extensions: js,mjs,json
api | [nodemon] starting `yarn rw-api-server-watch`
$ /workspaces/force-redwood-test/node_modules/.bin/rw-api-server-watch
gen | Generating TypeScript definitions and GraphQL schemas...
gen | 31 files generated
api | Building... Took 407 ms
api | Starting API Server...Importing Server Functions... 
api | /healthz 1 ms
api | Took 194 ms
api | Listening on http://localhost:8911/
api | INFO [2022-01-05 21:36:39.464 +0000]: Server listening at http://[::]:8911
api | (node:10337) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /workspaces/force-redwood-test/api/node_modules/node-fetch/src/index.js
api | require() of ES modules is not supported.
api | require() of /workspaces/force-redwood-test/api/node_modules/node-fetch/src/index.js from /workspaces/force-redwood-test/api/dist/services/sfCases/sfCases.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.
api | Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /workspaces/force-redwood-test/api/node_modules/node-fetch/package.json.
api | 
api |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
api |     at Module.load (internal/modules/cjs/loader.js:937:32)
api |     at Function.Module._load (internal/modules/cjs/loader.js:778:12)
api |     at Module.require (internal/modules/cjs/loader.js:961:19)
api |     at require (internal/modules/cjs/helpers.js:92:18)
api |     at Object.<anonymous> (/workspaces/force-redwood-test/api/src/services/sfCases/sfCases.js:2:19)
api |     at Module._compile (internal/modules/cjs/loader.js:1072:14)
api |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
api |     at Module.load (internal/modules/cjs/loader.js:937:32)
api |     at Function.Module._load (internal/modules/cjs/loader.js:778:12)
api | (Use `node --trace-warnings ...` to show where the warning was created)
api | (node:10337) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
api | (node:10337) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Does this make sense to anyone else? For reference I’m using Redwood.js v0.39.4 with Node v14.17.6.

Printing that out I do realize I need to update Redwood. I’ll do that and update this question.

1 Like

Just upgraded to the latest version and it still looks to be broken. :frowning:

api | Building... Took 398 ms
api | Starting API Server...Importing Server Functions... 
api | /healthz 1 ms
api | Took 178 ms
api | Listening on http://localhost:8911/
api | (node:17508) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /workspaces/force-redwood-test/api/node_modules/node-fetch/src/index.js
api | require() of ES modules is not supported.
api | require() of /workspaces/force-redwood-test/api/node_modules/node-fetch/src/index.js from /workspaces/force-redwood-test/api/dist/services/sfCases/sfCases.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.
api | Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /workspaces/force-redwood-test/api/node_modules/node-fetch/package.json.
api | 
api |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
api |     at Module.load (internal/modules/cjs/loader.js:937:32)
api |     at Function.Module._load (internal/modules/cjs/loader.js:778:12)
api |     at Module.require (internal/modules/cjs/loader.js:961:19)
api |     at require (internal/modules/cjs/helpers.js:92:18)
api |     at Object.<anonymous> (/workspaces/force-redwood-test/api/src/services/sfCases/sfCases.js:2:19)
api |     at Module._compile (internal/modules/cjs/loader.js:1072:14)
api |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
api |     at Module.load (internal/modules/cjs/loader.js:937:32)
api |     at Function.Module._load (internal/modules/cjs/loader.js:778:12)
api | (Use `node --trace-warnings ...` to show where the warning was created)
api | (node:17508) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
api | (node:17508) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

For anyone looking at this, I’ve just pushed the code to Github and made the repo public…

@MarkBennett Believe it or not @danny and I ran across this very issue a few days ago while working on a project and the issue is solved here: GitHub - node-fetch/node-fetch: A light-weight module that brings the Fetch API to Node.js

You have to use v2 and not v3 unfortunately:

‘npm install node-fetch@2’

Danny could probably explain better than I can, but with that change it works again.

1 Like

Thank-you! I’m giving that a try now and will report back.

My other theory is that one of my other deps is importing it elsewhere and there’s a conflict somehow. Will see! :slight_smile:

That looks to have fixed it, THANK-YOU! :slight_smile:

1 Like

:point_up_2: That is the key part of the error message. RW unfortunately doesn’t support pure ES module (ESM) packages.

I ran into this myself with the “string-strip-html” package codsen/packages/string-strip-html at main · codsen/codsen · GitHub

And RW itself with “pascalcase”

Interesting. Thanks for the clarification. Do you know why this is and if there’s an issue tracking it @Tobbe?

I don’t know if there is an issue for it. But if there isn’t, there should be!

It’s a pretty complicated topic

1 Like

I imagine! Do you want me to start an issue on the Redwood repo to track this?

If you don’t mind, that would be great! Thank you :slight_smile:

1 Like

Issue created @dthyresson!