Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngrx store selector failing on app import from custom library

I have an angular library with store implementation and this library is packaged as a NPM and used in different angular application.

I'm trying to use a ngrx store selector which was exported in the library in my different angular project and the application throws an error.

Module not found: Error: Can't resolve '@lib/core-lib/lib/core-store/store/account/account.selector' in 'C:\workspace\Client\AngularApp\src\app\app.component'

Angular Library Implementation:

import { createSelector } from '@ngrx/store';
import { ILibState } from '../../lib.state';
import { IAccountState } from './account.state';

const selectAccounts = (state: IAppState) => state.accounts;

export const selectSelectedAccount = createSelector(
  selectAccounts,
  (state: IAccountState) => state?.selectedAccount
);

exported this selector from the public_api.ts

export * from './lib/core-store/store/account/account.selector'
// Also tried this
// export { selectSelectedAccount } from './lib/core-store/store/account/account.selector'
// Also tried the barrel approach
// export * from './lib/core-store/store/account/index'

AngularApp Usage

app.component.ts

import { ILibState } from '@lib/core-lib/lib/core-store/lib.state';
import { select, Store } from '@ngrx/store';
import { takeUntil } from 'rxjs/operators';
import { selectSelectedAccount } from '@lib/core-lib/lib/core-store/store/account/account.selector';

this._store
  .pipe(select(selectSelectedAccount), takeUntil(this.destroy$))
  .subscribe((res) => {
    if (res && this.selectedAccountCode !== res) {
      this.selectedAccountCode = res.account;
    }
  });

Angular Library works fine without any issue and getting this piece of selector into my angular application is giving me error on compilation.

I tried to provide as much detail as possible and let me know if I need to add anything else.

Any answer on this is much appreciated

like image 473
imPK Avatar asked Oct 14 '22 22:10

imPK


1 Answers

Without some background on your folder structure and configuration it's going to be hard but let's stick to the error

Module not found: Error: Can't resolve '@lib/core-lib/lib/core-store/store/account/account.selector' in 'C:\workspace\Client\AngularApp\src\app\app.component'

This means that it can not find the file @lib/core-lib/lib/core-store/store/account/account.selector.ts which you intend to import

I suggest you first check the path is correct.

Besides that, there are a couple things I think you should double check:

  • Why are you using the alias @lib if you said this library is in an npm package?
  • Why are you doing deep imports if you exported those functionalities from public_api.ts?
like image 177
Ignasi Avatar answered Oct 21 '22 09:10

Ignasi