Member-only story

Understanding module.exports and exports in Node.js.

Beck Moulton
5 min readMay 2, 2024

--

Understanding module.exports and exports in Node.js.

In Node.jsprogramming, modulesare self-contained units of functionality that are shareable and reusable. They make our lives as developers easier because we can use them to enhance our applications without having to write functionality ourselves. They also allow us to organize and decouple our code, making applications easier to understand, debug, and maintain.

Different Node.jsModule Formats

Due to the fact that JavaScript initially did not have the concept of modules, various competing formats have emerged over time. The following are the main formats:

  • The Asynchronous Module Definition ( AMD) format is used in browsers and defines modules using define functions.
  • CommonJS( CJS) format is used in Node.jsand uses requireand module.exportsto define dependencies and modules. The npmecosystem is built on top of this format.
  • ESmodule ( ESM) format. Since ES6 (ES2015), JavaScriptsupports the native module format. It uses the exportkeyword to export the module's public API and the importkeyword to import it. The System.register format is designed to support ES6 modules in ES5.
  • The Universal Module Definition ( UMD) format can be used in browsers and Node.js. This is useful when a module needs to be imported by multiple different module loaders.

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet