Home > OS >  Require syntax equivalent of Import in Nodejs
Require syntax equivalent of Import in Nodejs

Time:05-31

I have an existing Nodejs application that still uses CommonJS, it's been fine up till now but I've ran into a module that I'm not sure how to import. I was hoping there was a fast way of doing it rather than restructuring my whole app to the module standard.

Here is the module's import documentation:

import MetaApi, {CopyFactory} from 'module.cloud-sdk';

const token = '...';
const api = new MetaApi(token);
const copyFactory = new CopyFactory(token);

I got the CopyFactory part to work by destructuring like so:

const { CopyFactory } = require('metaapi.cloud-sdk')

const copyFactory = new CopyFactory(token)

But I can't find a way to import the api aspect with the token, is there anyway of doing it?

Thanks a lot

CodePudding user response:

You can do it this way,

const MetaApi = require('metaapi.cloud-sdk');
const {CopyFactory} = MetaApi;

const token = '...';
const api = new MetaApi.default();
const copyFactory = new CopyFactory(token);

Hopefully this will work fine.

CodePudding user response:

As suggested by Bergi, adding default made it work

const { default: MetaApi, CopyFactory } = require(…)
  • Related