import (ES Modules): The modern approach, offering cleaner syntax, block-level scoping, and potential benefits like tree-shaking (removing unused code). To use import, you’ll need to enable ES modules in your project. This can be done by setting "type": "module" in your package.json or using the .mjs file extension.

require (CommonJS): The established method in Node.js, offering function-level scoping. While not as modern in syntax, it’s still widely used in existing projects.
When to Use Which
- New Projects: If you’re starting fresh,
importis generally recommended for its modern features and better fit with the latest JavaScript trends.
- Legacy Projects: If you’re working on an existing project that uses
require, there’s no immediate need to switch unless you specifically want to leverage ES module benefits. Tools like Babel can help bridge compatibility gaps if needed.