WhoAmI1000 / discord-oauth2-example

Example of a Discord OAuth2 Login System
https://circlertech.com/working-with-discord-oauth2
MIT License
28 stars 12 forks source link

node-fetch from v3 is an ESM-only module - you are not able to import it with require() #3

Closed johndoddsjr closed 2 years ago

johndoddsjr commented 2 years ago

node-fetch has been updated. With the current configuration I get the following error: $ node oauth.js /node_modules/node-fetch/src/index.js:9 import http from 'node:http'; ^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/oauth.js:7:15)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

Source: https://www.npmjs.com/package/node-fetch#installation node-fetch from v3 is an ESM-only module - you are not able to import it with require(). If you cannot switch to ESM, please use v2 which remains compatible with CommonJS. Critical bug fixes will continue to be published for v2. npm install node-fetch@2

I also found that replacing line 7 const fetch = require('node-fetch'); // Import node-fetch with const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

also gets past the error and loads.

WhoAmI1000 commented 2 years ago

Hello johndoddsjr,

Thanks for reporting this issue. I will look into it as soon as I can. It does appear from the docs that you can import the library asynchronously as you did; I'll update the tutorial after additionally confirming everything works well. Thank you for opening the issues you found! Your reports are greatly appreciated.