DEV Community

Cover image for STOP using require() in node backend
Harshkumar77
Harshkumar77

Posted on

5 2 1

STOP using require() in node backend

To all the node developer you need to stop using require() in your new project. The node has already support for modules and this tutorial will tell you difference between them and what to use instead of require() and also will deep down into module a bit more.

What are you using

const express = require('express') // common js
Enter fullscreen mode Exit fullscreen mode

What you should use

import express from 'express' // es module
Enter fullscreen mode Exit fullscreen mode

Did you see ?? how much the better code look in second one. The first one is commonjs syntax which is present in node from its origin for importing libraries , the second one was first introduced in browser and then it came to node.

It makes code so much readable , modern and non - verbose.

How to use it ?

Its easy.

  1. Initialise new node project.
  2. Go to your package.json.
  3. Add following to it.

    "type" : "module" ,
    
  4. By default when you initialise your project its set to commonjs.

  5. That's it now start using modern javascript.

Common patterns

Instead of explaining it I am going to show you commonjs code implemented in module format, so that you can start it immediately , also comeback to this article in future when you are confuse how to do certain things in module format.

Importing

// cjs
const express = require('express')
// mjs
import express from 'express'
Enter fullscreen mode Exit fullscreen mode
// cjs
const express = require('express')
const Router = express.Router
// mjs
import express , { Router } from 'express'
Enter fullscreen mode Exit fullscreen mode
//cjs
const clientRouter = require('express').Router
// mjs
import { Router as clientRouter } from 'express'
Enter fullscreen mode Exit fullscreen mode

Exporting

// cjs
module.exports = express
// mjs
export default express
Enter fullscreen mode Exit fullscreen mode
// cjs
module.exports = {
    router : {...} ,
    utils : {...}
}
// mjs
export {
    router : {...},
    utils : {...}
}
Enter fullscreen mode Exit fullscreen mode

Some more exporting pattern that may come handy

// mjs
export default function hello() {...}
export const bye = "bye"
Enter fullscreen mode Exit fullscreen mode

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (2)

Collapse
 
liftoffstudios profile image
Liftoff Studios

You are right about this but there is a problem that need to be addressed before using this immediately. What do you do when the module you use does not support import statements yet ? You cannot use require in that case as it throws errors.

Collapse
 
faizbyp profile image
Faiz Byputra

Thank you for your explaining

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay