Node.js
Modules Commands
Understand Node.js module systems — CommonJS (require) and ES Modules (import). Learn module resolution, creating packages, and migrating between module systems.
5 commands
Pro Tips
Use 'type: "module"' in package.json to enable ESM by default. Use .cjs extension for CommonJS exceptions.
Debug module resolution with 'NODE_DEBUG=module node app.js' to see exactly which files Node tries to load.
Use the 'exports' field in package.json to control which files are importable from your package.
Common Mistakes
You cannot require() an ES module. Use dynamic import() instead: 'const mod = await import("esm-package")'.
Circular dependencies in CommonJS return partially loaded modules. Refactor to break the cycle.