Twitter-API-v2-sample-code icon indicating copy to clipboard operation
Twitter-API-v2-sample-code copied to clipboard

Add version to `got` dependency

Open gonza7aav opened this issue 3 years ago • 1 comments

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'

gonza7aav avatar Jan 22 '22 04:01 gonza7aav

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

boahs avatar Jan 30 '22 07:01 boahs