Default import
import process from "node:process";
process.env.MY_VAR
- Brings in the default export of a module.
- Shorthand for
import { default as process } from "node:process"
.
- Returns the live object (including
this
context), so process.on
etc. work correctly.
- Usage: modules with a default export defined that require state.
Named import
import { env } from "node:process";
env.MY_VAR
- Brings only the explicitly exported property as a raw, static reference (
this = undefined
).
- Reduces bundle size (although tree-shakers are quite good at this, it will make compilation faster).
- Usage: pure or stateless functions and constants.
Namespace import
import * as process from "node:process";
process.env.MY_VAR
process.on(...) // Fails
- Collects all exports into a single frozen module namespace object (properties are immutable).
- Methods relying on
this
break (e.g. process.on
), since they’re just functions detached from the real object.
- Usage: static values/functions, not for stateful objects like
process
.