4

My project is written entirely in CommonJS module and I can't change it. I am trying to use a library which is an ESM. The library is Got library (https://github.com/sindresorhus/got).

This is my code

const request = require('request');
const Promise = require('bluebird');
// require('./wrapped-got');
var wrappedgot = null;

function HTTPRequestV2(hostURL, defaultOptions) {
    this.hostUrl = hostURL;
    this.requestWrapper = request.defaults(defaultOptions);
}


HTTPRequestV2.prototype.init = async function(){
    wrappedgot = await import('got');
    /*return import('got')
        .then(({default: theDefault}) => {
            wrappedgot= theDefault;
            console.log(theDefault);

        });
    console.log(wrappedgot);*/
};

But on running this I get error Not Supported on the wrappedgot = await import('got'); line

I tried using the work around of dynamic import function as suggested in their Issues page but its failing with above error https://github.com/sindresorhus/got/issues/1789

Even tried running their sample code but that is also failing with same error https://gist.github.com/szmarczak/0f2b70b2a1ed52a56a6d391ac02d094d

------Update-------

I am using Node Version 12.14.1, it supports async & await. I have read in SO where it has been used in Node Version 10

Using the dynamic import() function on Node.js

Got Library version is 13.0.0

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Vipresh
  • 1,250
  • 15
  • 31
  • have you tried importing got outside the function? At the top of the code? – AB7zz Jun 12 '23 at 14:30
  • Yes i tried that but its giving uncaught exception Uncaught /mnt/c/GitHome/Max/max-serving/src/classes/Utilities/HTTPRequestV2.js:6 Module._compile loader.js:891 Module._extensions..js loader.js:991 Module.load loader.js:811 Module._load loader.js:723 Module.require loader.js:848 require helpers.js:74 (anonymous function) DependencyInjector.js:143 Module._compile loader.js:955 Module._extensions..js loader.js:991 Module.load loader.js:811 Module._load loader.js:723 – Vipresh Jun 12 '23 at 14:38
  • `const request = require('request');` — Danger. [`request` was deprecated](https://www.npmjs.com/package/request) (marked "do not use") three years ago. – Quentin Jun 12 '23 at 14:50
  • "Not Supported" — Is that the complete error message? – Quentin Jun 12 '23 at 14:50
  • What version of Node.js are you using? – Quentin Jun 12 '23 at 14:50
  • Sounds like you're using an old version of node that doesn't support dynamic import. Upgrade your node to a newer version. – slebetman Jun 12 '23 at 18:14
  • @Quentin , I am using Node version 12.14.1, updated in answer – Vipresh Jun 13 '23 at 06:28
  • @Quentin, yes "Not Supported" is the complete error message, oter then that i get a long stack of loaders & modules up to the line in my class. – Vipresh Jun 13 '23 at 06:34
  • @Quentin, Yes we are trying to move from request library as its deprecated, if Got doesn't work i think we wont have much options left other then using the nodejs http object directly as the other popular library node-fetch has also migrated to ESM. – Vipresh Jun 13 '23 at 06:44

2 Answers2

0

Node.js 12.x does not support dynamic imports which were introduced with Node 13.2.0. Additionally, it has been over a year since security support for Node.js 12.x ended.

Upgrade to a current version of Node.Js.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

I'm pretty sure imports have to be in the global scope and you're trying to use an import inside of a function. You will have to move that import outside of that function in order for the error to go away. As a solution to this, import 'got' at the top with those requires and assign it to a variable and then when the function HTTPRequestV2.prototype.init is run then you should set 'wrappedgot' to whatever variable holds the 'got' import. I hope this helps.

<------------------ General improvements to this code ------------------>

Also this is unrelated but you've named a variable at the top 'Promise' and that should really be avoided since a promise is a constructor function in JavaScript and is used for something else, by using it's name for something else it could result in actual promises to throw an error.

I would also suggest not using 'var' in this day and age since it's a legacy version of JavaScript and can result in unexpected bugs that you might not expect unless you know what you are doing.

I would also recommend on using import instead of require to stay up to date with the latest implementations.

  • "I'm pretty sure imports have to be in the global scope and you're trying to use an import inside of a function" — You're incorrect. That's a [dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import). – Quentin Jun 12 '23 at 14:48
  • "As a solution to this, import 'got' at the top with those requires" — It won't work there. The question title says this is a CommonJS module (supporting `require`), not an ES Module (supporting the `import` keyword). – Quentin Jun 12 '23 at 14:49
  • Bluebird is a Promise library that implements a compatible API to promises. These days there's not much reason to use 3rd party promise library since most js engines have one built in but some people still do it for compatibility with old code since libraries like bluebird have extra features that built-in promises don't have. That declaration of `Promise` is correct, not a mistake. – slebetman Jun 12 '23 at 15:38
  • @whoLostEvee, I have used var only to make sure that it doesnt break because of scoping, otherwise u can see that when requiring other libraries I always use const. – Vipresh Jun 13 '23 at 06:31