Skip to content
Related Articles
Open in App
Not now

Related Articles

How to use EcmaScript modules in Node.js ?

Improve Article
Save Article
  • Last Updated : 07 Oct, 2021
Improve Article
Save Article

CommonJS modules consists usage of exports and require() statements, while EcmaScript modules consists usage of import and export statements.

Learn more here: Difference between node.js require and ES6 import and export

Node.js treats JS code as CommonJS modules by default, However the EcmaScript modules can be used instead of using –experimental-modules flag.

Follow the below mentioned Steps:

  • Initialize a package.json for Node.js project inside desired folder using following command, and enter values as prompted.

    npm init
  • Enter following command to skip prompts:

    npm init -y

  • Open newly created package.json and add following field.



  • File structure:

  • Example:


    const areaOfRectangle = (length, breadth) => {
        return length * breadth
    export default areaOfRectangle


    import areaOfRectangle from './area.js'
    console.log('Area of rectangle: ', areaOfRectangle(5, 8))


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!