When importing from node built-in modules, you can do it like this:
import fs from "node:fs;
or like this:
import * as fs from "node:fs;
Both of these work the same, and have no performance difference:
Benchmark 1: node defaultimport.mjs
Time (mean ± σ): 26.7 ms ± 0.5 ms [User: 22.3 ms, System: 4.0 ms]
Range (min … max): 25.9 ms … 28.4 ms 109 runs
Benchmark 2: node namespaceimport.mjs
Time (mean ± σ): 26.9 ms ± 0.7 ms [User: 21.3 ms, System: 5.3 ms]
Range (min … max): 25.9 ms … 30.6 ms 111 runs
Summary
'node defaultimport.mjs' ran
1.01 ± 0.03 times faster than 'node namespaceimport.mjs'
However, because of this, it is possible for a code base to have a mix of both import styles across different files. Thus, a lint rule is needed to force the code to be consistent.
For maximum flexibility, the rule could be figured to error on either pattern, allowing the end-user to pick their preferred style.
In the TypeScript ecosystem specifically, it is considered an anti-pattern to use default exports, so I imagine that most people would want to use this rule to enforce the * as
way, which would align the style of Node imports with all of the other imports.
I think that this rule would be a good fit for eslint-plugin-unicorn
because it is similar to the already-existing prefer-node-protocol rule.
import fs from "node:fs";
import * as fs from "node:fs";
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too