1

I've been struggling to setup path alias in my Node project using TypeScript. This is my package.json

{
  ...
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon",
    "build": "tsc"
  },
  ...
  "dependencies": {
   ...
    "node-cache": "^5.1.2",
    "nodemon": "^3.0.1",
    "passport": "^0.6.0",
   ...
  },
  "devDependencies": {
    ...
  }
}

... here is only to shorten.

Here's my nodemon.json:

{
  "watch": ["src"],
  "ext": "ts",
  "exec": "tsc && node dist/index.js"
}

And finally my tsconfig.json, where I define a path alias under paths

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ES2020",
    "rootDir": "./src",
    "moduleResolution": "Node",
    "baseUrl": "./",
    "paths": {
      "@util/*": ["./src/util/*"],
    },
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true
  }
}

So if I run the following code using npm start

import logger from '@util/logger';

it compiles correctly using the tsc command but when it runs the .js file I get the error

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@util/logger' imported from /path/to/my/project/dist/index.js.

If I replace '@util/logger' with './util/logger.js' it works just fine, but even if I remove the .js it throws me the erorr, only with Cannot find module instead of Cannot find package.

Not sure what am I missing, I even tried this:

// index.ts
import { fileURLToPath } from 'url'; // dirname workaround
import path from 'path'; // dirname workaround
import moduleAlias from 'module-alias';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
moduleAlias.addAliases({
  '@util': path.join(__dirname, 'util'),
});
import logger from '@util/logger';

but no luck, still the same error.

asallan3
  • 43
  • 5
  • Does this answer your question? [Typescript paths not working in an Express project](https://stackoverflow.com/questions/58187115/typescript-paths-not-working-in-an-express-project) – ghybs Jul 29 '23 at 01:40

2 Answers2

0

paths alias in tsconfig.json is only meant for the tsc compiler for types, not for the actually emitted JS code:

Note that this feature does not change how import paths are emitted by tsc, so paths should only be used to inform TypeScript that another tool has this mapping and will use it at runtime or when bundling.

If you do want to also affect the runtime imports, you can use the tsconfig-paths library:

Use this to load modules whose location is specified in the paths section of tsconfig.json

In your case, you could use it in your nodemon config like:

{
  ...
  "exec": "tsc && node -r tsconfig-paths/register dist/index.js"
}
ghybs
  • 47,565
  • 6
  • 74
  • 99
  • Tried with `tsconfig-paths` but no luck. What acually worked for me was this answer [link](https://stackoverflow.com/questions/58809944/cannot-find-module-typescript-path-alias-error) – asallan3 Jul 25 '23 at 18:08
0

What actually worked for me was this answer.

So I had to add

...
imports: {
   '#util/*': './dist/util/*'
}

to my package.json in order to work. But it only works with #, if I use @ it thinks it's a package for some reason and throws the same error.

asallan3
  • 43
  • 5