javascriptnode.jses6-modulescommonjsnode.js-got

How to import a Javascript file that is ESM from a CommonJS module? Got. Error: Not supported


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


Solution

  • 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.