2

I have an Angular 8 app that consumes a Service from a custom library I created.

The Service makes a HTTP call, so it has a dependency for an instance of the HttpClient in its constructor as follows:

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { User } from '../../models/user.model';
import { GetLoggedInUserInfo } from '../../models/get-logged-in-user-info';

@Injectable({
  providedIn: 'root',
})
export class AuthService {
  constructor(private http: HttpClient) {}

  getLoggedInUser = ({
    apiUrl,
    apiContextRoot,
  }: GetLoggedInUserInfo): Observable<User> => {
      const url = `${apiUrl}${apiContextRoot}/login`;

      return this.http.get<User>(url).pipe(
      );
    }
  };
}

The problem occurs when I invoke the getLoggedInUser() method of this service from the client app that custom installed the library via "npm install".

It seems that the dependency is not being fulfilled for the service in the custom library, which I thought would occur automatically.

main.ts:12 Error: inject() must be called from an injection context
    at injectInjectorOnly (core.js:728)
    at ɵɵinject (core.js:744)
    at AuthService_Factory (gulfstream-common.js:493)
    at _callFactory (core.js:30485)
    at _createProviderInstance (core.js:30428)
    at resolveNgModuleDep (core.js:30387)
    at NgModuleRef_.get (core.js:31577)
    at resolveDep (core.js:32142)
    at createClass (core.js:31988)
    at createDirectiveInstance (core.js:31806)

Any ideas?

Thanks

3
  • could you provide a way how your AuthService is used? Commented Apr 30, 2020 at 23:37
  • 1
    github.com/angular/angular/issues/25813 seems like the same issue like yours Commented Apr 30, 2020 at 23:42
  • The AuthService just makes an Http call to a Node.js back-end service that queries the database and returns a row of data for a particular user. Because that functionality is needed in every app that's created, I thought it best to put it in a Library that can be easily reused. Commented May 1, 2020 at 1:53

1 Answer 1

4

@Andrei provided the link github.com/angular/angular/issues/25813, which helped to resolve the issue.

I updated the angular.json file, of the app that consumes the library, and included "preserveSymlinks": true in 2 locations:

  1. under "architect"->"build"->"options"

  2. for each configuration under "architect"->"build"->"configurations"

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

This fixed my issues when trying to use libraries locally with npm link.

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.