Nx: node-apps: unable to import custom paths from tsconfig.json

Created on 14 Nov 2018  ยท  11Comments  ยท  Source: nrwl/nx

I have following setup for my node-app
when I run ng serve api i am getting Module not found error

tsconfig.json

    "paths": {
      "@env-api/*": [
        "apps/api/src/environments/*"
      ],
}

config.service.ts

import { environment } from '@env-api/environment';

ERROR

ERROR in ./apps/api/src/config/config.service.ts
Module not found: Error: Can't resolve '@env-api/environment' in '/.../ngx-starter-kit/apps/api/src/config'

similar setup works for angular webapps

repro needed bug

All 11 comments

What does your tsconfig.app.json file look like in your node-app?

tsconfig.app.json for node-app

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/apps/api",
    "module": "commonjs",
    "target": "es6",
    "types": ["node"]
  },
  "exclude": ["**/*.spec.ts"],
  "include": ["**/*.ts"]
}

This is an odd error message:

Module not found: Error: Can't resolve '@env-api/environment' in '/.../ngx-starter-kit/apps/api/src/config'

Can you please provide a repro so we can look deeper?

I have to change "apps/api/src/environments/*" to "apps/api/src/environments/environment.ts"
to make it work. but wonder how it is working for webapps!

https://github.com/xmlking/ngx-starter-kit/blob/develop/tsconfig.json

      "@env-api/environment": [
        "apps/api/src/environments/environment.ts"
      ],

I am also trying to make e2e tests work for API, but getting following error with ng test api-e2e
may be related issue!

  โ— Test suite failed to run

    Cannot find module '@env-api/environment' from 'config.service.ts'

      1 | import { Injectable } from '@nestjs/common';
    > 2 | import { environment } from '@env-api/environment';
        | ^
      3 | 
      4 | // tslint:disable-next-line
      5 | const packageJson = require('../../../../package.json');

      at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (../api/src/config/config.service.ts:2:1)

hi @xmlking

Had the time to have a quick look and came to this, maybe it is useful for you.

When you use a * in the paths entry of a tsconfig file the module resolution would be like this:

The example tsconfig.json would look like:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": [
        "*",
        "generated/*"
      ]
    }
  }
}

This tells the compiler for any module import that matches the pattern * (i.e. all values), to look in two locations:

  • *: meaning the same name unchanged, so map <moduleName> => <baseUrl>/<moduleName>
  • generated/* meaning the module name with an appended prefix generated, so map <moduleName> => <baseUrl>/generated/<moduleName>

I think this is why the change to apps/api/src/environments/environment.ts was required (only 1 * in your config entry that can't be replaced with a <moduleName>). Usually you reference an index.ts and export all the stuff there.

About the e2e issue, I think this line is the key here, does not seem ideal ... I think the source is not included via the tsconfig.e2e.json? But I would rethink that setup, I think you can get rid of // @ts-ignore ...

... just saw that you had 2 *, so maybe it was not that concerning the 1st error ...

@xmlking Did you ever figure out why this was happening? I'm trying to move an existing project into an nx workspace and I've come to the same issue. Angular web apps work, nestjs server apps do not.

@jmcdo29 not yet found the cause , but you might find some workarounds from my repo.

@FrozenPandaz apps/angularApp --> libs/shared works with paths in tsconfig.json same logic for apps/nestjsApp --> libs/shared didn't work. are we missing some config in nodejs builders ?

@jmcdo29 @xmlking fix is coming soon. I experienced the same issue.
The reason for this is angular builder implements a different path resolution library than the nx builder which is used for non-angular projects & libraries. @FrozenPandaz has a fix in the makings (thx!) and it looks like all tests have passed and it's going to be merged soon! :)

Was this page helpful?
0 / 5 - 0 ratings