alakazam5034
alakazam5034

Reputation: 23

Require syntax equivalent of Import in Nodejs

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

Upvotes: 1

Views: 119

Answers (2)

Ahmad ul hoq Nadim
Ahmad ul hoq Nadim

Reputation: 378

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.

Upvotes: 2

alakazam5034
alakazam5034

Reputation: 23

As suggested by Bergi, adding default made it work

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

Upvotes: 0

Related Questions