An eslint rule no-var-requires
in a recent project forces me to write import = require()
. I have never seen this until now and I never had any issues with importing, but reading about it reveals that this was a thing in 2017 already. This makes me wonder why would anyone enforce this - or better, why is it considered a bad practice to use require()
and what is the actual difference between
export default class ZipCodeValidator { }
export = class ZipCodeValidator { }
where the latter is, in my understanding, directly connected to the import = require()
syntax in TS?
The documentation about export =
and import = require()
^ says that
Both CommonJS and AMD support replacing the exports object with a custom single object. Default exports are meant to act as a replacement for this behavior; however, the two are incompatible.
But all these work just fine
require('dotenv').config() // this is what I used originally
import dotenv = require('dotenv') // the eslint rule forces me to write this
dotenv.config()
const dotenv = require('dotenv')
dotenv.config()
import * as dotenv from 'dotenv' // This is what I've been using otherwise
dotenv.config()
This makes me wonder about the purpose of import = require()
in the first place.
Instead of just using the sleek unobtrusive require
with a call in one line, it forces me to split the importing and invocation in two lines, which in turn brings another complication. We have a lint/prettier rule to organize imports to the top, but this makes the other imported modules run BEFORE my environment is initialized.
Forgive me for asking in such vague manner, but I don't even know where to start.