Tsconfig `paths` equivalent for normal JS node

Viewed 55

I have this on a TypeScript project, it's a nice config option on tsconfig.json that lets me create aliases for the folders

{
"compilerOptions": {
  "paths": {
    "@src/*": ["src/*"],
    "@images/*": ["src/assets/images/*"],
    "@styles/*": ["src/assets/styles/*"],
    "@components": ["src/components"],
    "@layout/*": ["src/layout/*"],
    "@pages/*": ["src/pages/*"]
  }
}

Is there a way to do the same on a project that does not use TypeScript, just regular Javascript in node with ES6 modules on?

1 Answers

I found that node already has something called Subpath imports, is not exactly the same but it works for me. For anyone looking for something similar, this just uses # instead of @:

on the package.json

"imports": {
  "#src": "./src/*",
  "#controllers/*": ["./src/controllers/*"],
  "#routes/*": ["./src/routes/*"]
}

And then import on your app, – In this case I use ES modules:

import File from '#routes/file.js'
Related