admin管理员组

文章数量:1317906

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


  "pilerOptions": {
    "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 mands is:

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

tsc will pile 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?

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


  "pilerOptions": {
    "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 mands is:

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

tsc will pile 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?

Share Improve this question asked Oct 30, 2019 at 21:44 jamesdeath123jamesdeath123 4,61613 gold badges54 silver badges95 bronze badges 10
  • 1 Does the error only occur if you build and run the app? – Daniel Habenicht Commented Oct 30, 2019 at 21:57
  • @DanielHabenicht the build works fine, it only happens on run time. – jamesdeath123 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. – Daniel Habenicht Commented Oct 30, 2019 at 23:24
  • That's needed because the typescript piler 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. – Daniel Habenicht Commented Oct 30, 2019 at 23:30
  • If that solved it for you I will right an answer, so please let me know. – Daniel Habenicht Commented Oct 30, 2019 at 23:58
 |  Show 5 more ments

2 Answers 2

Reset to default 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 piled 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 .

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

本文标签: javascripttypescript path alias needs modulealiasregister on every fileStack Overflow