Epanastatis
Epanastatis

Reputation: 13

Using 'require' with esm - ReferenceError: require is not defined

I have been updating my codebase to replace instances of 'require' with import syntax as per esm guidelines, but have hit a snag with a few examples where I'm not sure what the correct syntax is:

const router = require('express').Router();
require('dotenv').config();

What is the correct way to convert these to import syntax?

Upvotes: 1

Views: 1206

Answers (1)

Yousaf
Yousaf

Reputation: 29334

You can get a reference to express router by first importing the express package

import express from "express";

and then accessing the Router property on it

const router = express.Router();

You can do the similar thing for dotenv package.

// import the "dotenv" package
import dotenv from "dotenv";

// call the config function
dotenv.config();

Upvotes: 4

Related Questions