# Converting CommonJS modules to ES in JavaScript

Still in various documentation, you can find importing in the CommonJS modules style. You need to learn how to convert such an import into standard ES modules.

In CommonJS, import is performed using the `require` command, the parameter of which is a path to module. The result of the command is written to a variable:

`let math = require('./math');`

Let's rewrite this code in ES style. First of all, pay attention to the fact that in CommonJS the `.js` extension is not set for files, but in ES it is.

Further, it all depends on whether export from a module is by default or regular. There is no difference in CommonJS, but there is in ES modules. As a rule, the difference is visible by code examples, or you can simply try both import options.

In our case, the above import will be rewritten either in this form:

`import math from './math.js';`

Or in this one:

`import * as math from './math.js';`

Rewrite the following code in the ES module style:

`let {square, cube} = require('./math');`

Rewrite the following code in the ES module style:

`let math = require('./math'); let res = math.square(2) + math.cube(3);`

Rewrite the following code in the ES module style:

`let sum = require('./sum'); let res = sum([1, 2, 3]);`
←→
↑
en ru