How to call constructor function when using ESM import syntax in nodejs?

Viewed 223

in NodeJS you can 'import' an external file like so:

var myModule = require('./module.js');

ESM modules allow you to do the same in nodejs as you do in the backend...

import myModule from './module.js';

However, with the require syntax, I can call a constructor function (forgive if my term is wrong) like so:

var myModule = require('./module.js')(myObject);

... how would one do this with ESM syntax?

1 Answers

I really don't think this is possible, I think you just have to use

import MyModule from './module';
const myModule = MyModule();

For this reason I use PascalCase for modules, even though that's not the normal convention.

Related