Which should have .mjs extension, the importing file or the exporting file, or both?

2.1k Views Asked by At

I have a silly doubt. Which file, the importing file, the exporting file or both should have the .mjs file extension?

Specifically for NodeJS.

I have searched the web but didn't get my exact answer.

Thanks in advance!

1

There are 1 best solutions below

2
On

I think just the one that uses the import should be in .mjs. From the docs:

An import statement can reference an ES module or a CommonJS module. import statements are permitted only in ES modules, but dynamic import() expressions are supported in CommonJS for loading ES modules.

When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as a convenience for better ecosystem compatibility.