Closed
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the rule's documentation
https://typescript-eslint.io/rules/no-require-imports/
Description
If module
in tsconfig.json
is set to Node16
, TypeScript 4.7 will convert import … = require('…')
statements to require calls created using _createRequire
.
As long as import assertions are not allowed in Node.js, I believe this is a nice syntax to read package.json
files.
I propose to add an option named allowJson
to no-require-imports
that allows require imports of JSON files.
Fail
import lodash = require('lodash')
import lib = require('./lib')
Pass
These are currently also errored on
import lodashPkg = require('lodash/package.json')
import pkg = require('./package.json')
Additional Info
In:
import pkg = require('./package.json')
console.log(pkg.version)
Out:
import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const pkg = __require("./package.json");
console.log(pkg.version);