nx icon indicating copy to clipboard operation
nx copied to clipboard

Nx workspace libs paths issue

Open xeax opened this issue 8 years ago • 11 comments

I'm trying to convert my Angular 4.4 single SPA to Nx workspace.

I've prepare my SPA for migration by extracting 2 libraries ("core" in "src/app/core" & "fake-db" in "src/app/fake-db") and adding path alias for them in tsconfig.json: "paths": { "@mc/*": [ "app/*" ] }.

Converting SPA successfull, "ng serve" and "ng build --prod" works fine.

Then I move libs to libs/* and rewrite tsconfig.json "paths" to { "@mc/*": ["app/../../../../libs/*"] }. "ng serve" still work fine, but "ng build --prod" crashes with multiple errors, like this: ERROR in G:/mc/apps/myapp/src/$$_gendir/app/app.module.ngfactory.ts (21,22): Cannot find module '../../../../../libs/core/components/confirm-dialog/confirm-dialog.component.ngfactory'.

I've tried many "baseUrl" and "paths" combinations, but no one worked for libs outside of app folder.

xeax avatar Feb 19 '18 15:02 xeax

Temporary solved issue with symlink "libs" in "apps/myapp/src" linked to "libs" in Nx workspace root folder and setting tsconfig.json "paths" to { "@mc/*": [ "libs/*" ] }.

xeax avatar Feb 19 '18 15:02 xeax

@xeax which version of nx and angular-cli are you using?

Yonet avatar Feb 21 '18 02:02 Yonet

@nrwl/nx: 0.7.4,
@angular/cli: 1.4.10

xeax avatar Mar 07 '18 04:03 xeax

@xeax - please retry with @nrwl/nx: 0.8.0 (which includes @angular/cli 1.7.2)

ThomasBurleson avatar Mar 07 '18 16:03 ThomasBurleson

@xeax to be honest, I would create a clean workspace and copy the ng modules to their desired places in your future nx libs/apps (never upgraded myself) - you do that anyway (copying your core, fake-db and other modules to the locations you want them, adopt the imports ...).

Still, I don't understand your Typescript setup. Thought I had an issue myself concerning the setup (e2e related), but have to check that again...

In a new workspace the Typescript setup looks like this (not sure how yours looks like):

  • myworkspace
    • tsconfig.json (this one is including "paths": { "@myworkspace/*": [ "libs/*" ])
    • apps
      • myapp
        • src
          • tsconfig.app.json (extending tsconfig.json)
          • app (containing app.module.ts, the main entry point for the ng modules of that app)
        • e2e
          • tsconfig.e2e.json (also extending tsconfig.json)
    • libs
      • mylib
        • index.ts (including the barrel exports)
        • src (the main entry point for the ng modules of that lib)

the path entry { "@mc/*": ["app/../../../../libs/*"] }, it seems like that your tsconfig.json (should be tsconfig.app.json to align with the default) is located under myworkspace/apps/myapp/src/, but the definitoin of the path entry belongs to tsconfig.json under myworkspace (if you want to align with the default). you can have a look at this workspace (or any other example workspace from nx) for a quick look how the setup is ment to be. maybe #334 is helpful too, concerning your future import statements...

skydever avatar Mar 15 '18 22:03 skydever

@xeax just watched the free courses at angularplaybook.com, did you do it like shown there? great introduction to nx from the nrwl team!

skydever avatar Mar 16 '18 22:03 skydever

I checked the e2e related issue I was talking about, somehow I can't make it work using imports like @myworkspace\mye2elib from within myapp\e2e\app.e2e-spec.ts. Currently I am using ../../../libs/mye2elib and I added ../../../libs/mye2elib to the allow section of the lint rule - everything works fine, but it would be nice to use the barrel imports there too (and avoid the allow entry). When I use @myworkspace\mye2elib I geht the following error:

[17:02:36] E/launcher - Error: Error: Cannot find module '@myworkspace/mye2elib'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    ...

The background (short version) - I created something like an app shell lib every app in my workspace is using (handle settings like language, theme ...). I have another lib mye2elib where I created the e2e tests for this features. In all of my apps I added the e2e tests from mye2elib to check the app shell features (they are accessible via a toolbar every app is using, so I can reuse the e2e tests). Looks like this in app.e2e-spec.ts:

import { e2eSettings } from '../../../libs/mye2elib';

...

const appName = 'My App';

describe(`${appName} App`, () => {

...

  describe(`${appName} Settings`, () => e2eSettings(browser.baseUrl));

});

and like this in the lib mye2elib:

export function e2eSettings(testUrl: string = '/') {
  let page: AppPage;
  let settings: SettingsPage;

  beforeEach(() => {
    page = new AppPage(testUrl);
    settings = new SettingsPage();
  });

  describe('Settings Dialog', () => {
    it('should have the title "Settings" when language is set to english', () => {
      page
        .navigateTo()
        .then(() => settings.openSettingsDialog())
        .then(() => settings.setLanguageEnglish())
        .then(() => expect(settings.getSettingsDialogTitle().getText()).toEqual('Settings'));
    });

...

Is it ok to reuse e2e tests like this, or is it somehow a bad practice? ...and any ideas why the barrel imports don't work? I thought because of the inheritance in the tsconfig this should be possible - also tried to add paths to tsconfig.e2e.json, changed the include section, but with no luck ...

skydever avatar Mar 21 '18 16:03 skydever

We have just tried to upgrade our existing cli app to nx, and we get the same issue with `ERROR in apps/mediamanager/src/app/app.component.ts(9,58): error TS2307: Cannot find module '@digizuite/services'``. I have tried with both the nx version specified by running the automatic upgrade scripts, and with the latest version of both the nx, cli and angular. Neither of which makes things work.

zlepper avatar Mar 24 '18 14:03 zlepper

@zlepper could you provide a link to a repro?

vsavkin avatar Mar 29 '18 17:03 vsavkin

Closing this. If the issue is present in Nx 1.0, please reopen it with a repro.

vsavkin avatar Apr 25 '18 13:04 vsavkin

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

github-actions[bot] avatar Mar 25 '23 21:03 github-actions[bot]