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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay