Back to case studies
Node.jsFundamental
Creating and using npm modules with CommonJS and ES modules
Scenario
Context A utility library with math helper functions needs to be shared across multiple Node.js scripts in the same project. The Problem All utility functions are copy-pasted into every script that needs them, creating maintenance inconsistencies. Constraints Create a utils.js module exporting at least three functions. Import the module in another file using require() (CommonJS). Convert both files to use ES module syntax (import/export) and update package.json accordingly. Expected Deliverable A Node.js utility module demonstrating both CommonJS and ES module syntaxes, with correct package.json configuration for each approach.
Targeted skills
nodejsmodulescommonjsesmodulesnpm