6

I am using typescript v3.6.4 with the following tsconfig.json snippet:


  "compilerOptions": {
    "moduleResolution": "node",
    "baseUrl": "./src",
    "paths": {
      "@config/*": ["config/*"],
      "@config": ["config"],
    }
  }

and module alias in package.json:

  "_moduleAliases": {
    "@config": "dist/config"
  }

I have the following folder structure:

src
 |-config
     |-index.ts
 |-app
     |index.ts
     |logic.ts
 |-dist

Now in app/index.ts, if I do:

import 'module-alias/register';
import config from '@config';

and my npm start commands is:

"start": "node -r ts-node/register ./src/app/index.ts",

tsc will compile successfully but npm start will give error:

Error: Cannot find module '@config' in src/app/logic.ts

And the only way to fix this is to also add

import 'module-alias/register';

in src/app/logic.ts

Seems I have to add the import 'module-alias/register' in every file that I do alias? Is it a way to configure this?

10
  • 1
    Does the error only occur if you build and run the app? Commented Oct 30, 2019 at 21:57
  • @DanielHabenicht the build works fine, it only happens on run time. Commented Oct 30, 2019 at 22:48
  • 1
    That's what I meant. You have to import the module-alias/register only once in you app/index.ts it should load all other imports in e.g. in app/logic.ts without the need to import it there again. That's also what their docs say. Commented Oct 30, 2019 at 23:24
  • That's needed because the typescript compiler does not update the paths in the generated files (with what you specified in the tsconfig.ts). That's why you need to load module-alias/register and of course in order to make it work you have to at least import in once, otherwise the packages code would not be included. Commented Oct 30, 2019 at 23:30
  • If that solved it for you I will right an answer, so please let me know. Commented Oct 30, 2019 at 23:58

2 Answers 2

3

@2021

For me I've got exact the same issue: typescript allowed me only use the module aliasing when the every module has import 'module-alias/register'; at its top line when it has such module alias.

After spent nearly whole day, I got the point:

tsc won't help to transpiling these decorators. so you have to make sure every consumer of the js compiled code have acknoledged and know how to deal with these module imports with alias.

i.e.:

node index.js
=>
node -r module-alias/register index.js

///

mocha .
=>
mocha -r module-alias/register .
Sign up to request clarification or add additional context in comments.

Comments

0

The solution to this is quite simple:

in the root file, like app/index.ts, put the import 'module-alias/register'; to the end of all the other imports and you won't need to put it in every file. It didn't work for me because I put that import before all other imports, not after.

For example:

import express from 'express';
import corsFilter from 'cors';
...
import 'module-alias/register'; // This has to be after all other imports

1 Comment

This is the opposite of what is recommended on their README fyi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.