Video
https://youtu.be/F3bzrxSJJLo
Blog
JavaScript imports guide (Everything you need to know, in 5 minutes)
Default import
import process from "node:process";
process.env.MY_VAR
- Imports the module’s default export (if one exists).
- Shorthand for
import { default as X } from "Y".
- If the default export is an object (for example the
process object in Node.js), calling methods via that object (e.g., process.on(...)) uses the correct object as the this context.
- Use this when the module exports a stateful object or one “main” entity.
Named import
import { env } from "node:process";
env.MY_VAR
- Brings only the explicitly exported property as a raw, static reference.
- If you import a function or method via named import and then call it without its owning object, you may lose the original this binding (so
this may be undefined).
- Reduces bundle size (although tree-shakers are quite good at this, it will make compilation faster).
- Still runs code in the imported module.
- Usage: pure or stateless functions and constants, reducing import size.