Node.js Just Got Cooler: Imports and Package JSON Are Here to Save Your Day


If you’ve ever worked on a Node.js project, you’ve probably had this experience:

You’re writing an import, your brain says “easy-peasy”, but your editor whispers:

import service from "./../../../path/to/some/service.ts";
Enter fullscreen mode

Exit fullscreen mode

Suddenly, you’re not coding anymore. You’re Indiana Jones, lost in a temple of ../ and praying you don’t accidentally end up in the wrong directory.

Well, guess what? Node.js just threw us a lifeline. It’s called Imports via package.json, and it’s here to kick those ../../../ nightmares out of your life.




Before: The Jungle of Relative Paths

Back in the day, importing a file felt like playing “how many dots can you fit in a path?”

import service from "./../../../path/to/some/service.ts";
Enter fullscreen mode

Exit fullscreen mode

This is fine when your project is three files and a dream. But once it grows, you’re basically navigating with Google Maps set to “maze mode.”




After: The Alias Glow-Up

Here’s where the magic happens. Open up your package.json, sprinkle in a little configuration, and suddenly your imports are as smooth as butter.

"imports": {
  "#src/*": "./src/*"
}
Enter fullscreen mode

Exit fullscreen mode

That’s it. Seriously. Just one tiny block and your codebase is already breathing easier.




Usage: Imports, But Make Them Sexy

Now instead of typing a path that looks like a family tree gone wrong, you just do this:

import service from '#src/path/to/some/app.ts';
Enter fullscreen mode

Exit fullscreen mode

Clean. Simple. Understandable.
And if someone asks, “where’s that service coming from?” you don’t have to pull out a whiteboard and draw arrows through directories.




Why This Rocks

  1. No More Dot Gymnastics – Your pinky finger finally gets a break from all those ../ key presses.
  2. Readable Code – Your imports now explain themselves, instead of looking like cryptic treasure maps.
  3. Future-Proof – This is modern Node.js flexing its muscles. You’re not just writing code, you’re writing fancy code.



Bonus: Pair It with Watch Mode

Check out my other piece: Node.js Just Got Cooler: Native TypeScript and Watch Mode Are Here to Party.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *