Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Node.js Module Import

Node.js Module Import

When you start writing a Node.js project, one of the first problems you encounter is how to import a module.

In Node.js, modules are reusable JavaScript files that communicate with each other through imports and exports.

The Node.js ecosystem mainly supports two module systems:CommonJS and ** ES Modules (ESM)**.

Module System Convention File extension Import method Export methods
CommonJS Node.js initially used .js require() module.exports / exports
ES Module (ESM) the ECMAScript standard .mjs or "type": "module" import export / export default

the CommonJS module system

CommonJS is the earliest and most widely used module system in Node.js. It uses synchronous loading, which means that a module blocks program execution while loading until the load is complete.

Import and export

In CommonJS, you use the require() function to import modules and the module.exports or exports object to export modules.

Export method:

Code in the math.js file:

// math.js

function

add

(

a

,

b

)

{

 

return

a

b

;

}

module.

exports

=

{

add

}

;

// Or: exports.add = add;

Import method:

Example

// app.js

const

math

=

require

(

'./math.js'

)

;

console.

log

(

math.

add

(

2

,

3

)

)

;

// 5

For a module to expose its contents to the outside, it needs to use module.exports or exports to export them:

Example

// user.js

const

name

=

'Alice'

;

const

age

=

30

;

// Method 1: use module.exports to export a single object or value

module.

exports

=

{

  name

:

name

,

  age

:

age

,

  sayHello

:

(

)

=>

{

    console.

log

(

`Hello

,

my name is $

{

name

}

.`

)

;

 

}

}

;

// Method 2: use exports to export multiple named variables

// exports is a reference to module.exports

exports.

name

=

name

;

exports.

age

=

age

;

exports.

sayHello

=

(

)

=>

{

  console.

log

(

`Hello

,

my name is $

{

name

}

.`

)

;

}

;

Import method:

Example

// main.js

const

user

=

require

(

'./user.js'

)

;

console.

log

(

user.

name

)

;

// Output: Alice

user.

sayHello

(

)

;

// Output: Hello, my name is Alice.

// You can also destructure directly

const

{

name

,

age

}

=

require

(

'./user.js'

)

;

console.

log

(

name

)

;

// Output: Alice

Features of CommonJS:


The ES Module (ESM) Specification

ESM is the official module standard for JavaScript. It uses asynchronous loading and is the preferred choice for browsers and modern Node.js applications.

In ESM, you use the import statement to import modules and the export statement to export modules.

**Enable ESM:** To use ESM in Node.js, you need to add "type": "module" to your package.json file, or change the file extension to .mjs.

// package.json

{

  "type": "module"

}

Export a module:

Example

// math.mjs

export

function

add

(

a

,

b

)

{

 

return

a

b

;

}

export

default

function

multiply

(

a

,

b

)

{

 

return

a

b

;

}

Import a module:

Example

// app.mjs

import

multiply

,

{

add

}

from

'./math.mjs'

;

console.

log

(

add

(

2

,

3

)

)

;

     

// 5

console.

log

(

multiply

(

2

,

3

)

)

;

// 6

** How to enable:**

{ "type": "module" }

ESM supports two export methods: Named Exports and Default Export.

Example

// user.mjs

// Named export

export

const

name

=

'Bob'

;

export

const

age

=

25

;

// Default export

const

sayHello

=

(

)

=>

{

  console.

log

(

`Hello

,

my name is $

{

name

}

.`

)

;

}

;

export

default

sayHello

;

Import method:

Example

// main.mjs

// Import a named export

import

{

name

,

age

}

from

'./user.mjs'

;

console.

log

(

name

)

;

// Output: Bob

// Import the default export

import

sayHello from

'./user.mjs'

;

sayHello

(

)

;

// Output: Hello, my name is Bob.

// Import both named and default exports

import

sayHello

,

{

name

}

from

'./user.mjs'

;

console.

log

(

name

)

;

sayHello

(

)

;

// Import all named exports as properties of an object

import

as user from

'./user.mjs'

;

console.

log

(

user.

name

)

;

user.

default

(

)

;

// The default export becomes the default property

Features of ESM:


Differences Between CommonJS and ESM

Projects CommonJS ESM
Syntax require / module.exports import / export
Loading mechanism synchronous loading at runtime static loading at compile time
Default support supported by default in Node.js requires .mjs or "type": "module"
Suitable scenarios Backend scripts, legacy projects Modern frontend and backend projects, Tree-shaking
Can they be mixed? Cannot be mixed directly (requires extra configuration) Cannot be mixed directly (requires extra configuration)

Mixed usage

Newer versions of Node.js support the coexistence of both module systems. You can use CommonJS and ESM together in the same project, but you need to note the following:

// commonjs_module.js module.exports = { data: 'hello' };

// esm_module.mjs import commonModule from './commonjs_module.js'; console.log(commonModule.data); // Output: hello

// esm_module.mjs export const name = 'Bob';

// commonjs_module.js async function loadESM() { const { name } = await import('./esm_module.mjs'); console.log(name); } loadESM();

Best Practice Recommendations

For new projects,ES Modules (ESM) are strongly recommended. It is not only the official standard for JavaScript, but also more compatible with modern front-end toolchains (such as Vite and Next.js), and can fully leverage optimization techniques such as Tree Shaking to reduce the size of bundled code.

If you are maintaining an old CommonJS project and do not need ESM features, you can continue using CommonJS. But if you need to introduce new dependencies or take advantage of new ESM features, you can consider a gradual migration or use a mixed mode for transition.