How to create a CJS module
Published: 2022-10-21 09:44:09

---
title: How to create a CJS module
published: true
date: 2022-02-01 00:00:00 UTC
tags: 
canonical_url: https://www.mariokandut.com/how-to-create-a-cjs-module/
---

> This article is based on **Node v16.14.0**.

Modules are building blocks of code structures and allow Node.js developers to build better structure, reuse, and distribute code. Since **Node v14** , there are two kinds of modules, **CommonJS Modules (CJS)** and **EcmaScript Modules (ESM)**. This article is about CommonJS modules (CJS).

When importing a CJS module, you have to `require` it. The result of `require` is in some cases a function, which creates a new instance, but this is not guaranteed. The `require` function will always return what is exported from the module.

Let's create a local module, which will convert any input string to uppercase and use it in **five steps**.

1. Create a file for the module.

```
touch format.js
```

1. Create a simple function which converts any input string to uppercase.

```
'use strict'
const toUpper = str => {
  if (typeof str === 'symbol') str = str.toString(); // convert to string if symbol is used as input
  str += '';
  return str.toUpperCase();
};

module.exports = { toUpper };
```

1. Create an `index.js` to import the CSJ module.

```
touch index.js
```

1. Import module with destructuring.

```
const { toUpper } = require('./format');
console.log(toUpper('this will be uppercase'));
```

1. Execute it.

```
node index.js
```

The output to the terminal should be `THIS WILL BE UPPERCASE`.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- The `require` function will only return what is exported from the module.
- CommonJS modules are imported with the `require` function.
- Modularization helps to abstract complexity.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[JSNAD](https://trainingportal.linuxfoundation.org/),[CommonJS](https://requirejs.org/docs/commonjs.html)