module.exports = () => {
console.log('Learning NodeJs.');
};
index.mjs
import sampleModule from './sampleModule.mjs';
sampleModule();
Execution Error:
% node --experimental-modules index.mjs
file:///Users/c2ctechtv/Desktop/node-examples/index.mjs:1
import sampleModule from './sampleModule.mjs';
^^^^^^^^^^^^
SyntaxError: The requested module './sampleModule.mjs' does not provide an export named 'default'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:122:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:188:5)
at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24)
at async loadESM (node:internal/process/esm_loader:40:7)
at async handleMainPromise (node:internal/modules/run_main:66:12)

Fix:
This error occurs when you try to import a default export from an ES module, but the module does not contain a default export named 'default'.
We make the following changes to our files to fix this error,
SampleModule.mjsexport const message = 'Learning NodeJs.';
index.mjs
import { message } from './sampleModule.mjs';
console.log(message);
Execution:
% node --experimental-modules index.mjs
Learning NodeJs.
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!