xdevplatform / Twitter-API-v2-sample-code

Sample code for the Twitter API v2 endpoints
https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api
Apache License 2.0
2.65k stars 984 forks source link

Add version to `got` dependency #76

Open gonza7aav opened 2 years ago

gonza7aav commented 2 years ago

If we just copy the dependencies installation code from JavaScript (Node.js) environment set up, we'll get the following

npm install needle
npm install got
npm install oauth-1.0a

This means we are installing the latest version of got. Since v12.0.0 the package is pure ESM, so we must use import to load it or we'll get the following error

~/index.js:3
const got = require('got');
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module ~/node_modules/got/dist/source/index.js from ~/index.js not supported.
Instead change the require of ~/node_modules/got/dist/source/index.js in ~/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (~/index.js:3:13) {
  code: 'ERR_REQUIRE_ESM'
}

To reproduce the error, you need to install the dependencies the way the README.md says and try to run any example that requires got package.

There are two solutions as I see, we could rewrite all the scripts that fail or we could install a previous version to v12.0.0. So in order to run the examples as they are without any script change, I suggest adding a version range with a "less than" symbol.

npm install got@'<12.0.0'
boahs commented 2 years ago

Installing got at an earlier version allowed me run the sample code finally. Thanks for the fix @gonza7aav