Multiple Angular libs do not work in production mode

Viewed 203

I've created an Angular project which consists of two Angular libs. You can find the source code here. After that, I've built eaсh library and share the resulting dist folder via npm. Here is the package: https://www.npmjs.com/package/@agilie/angular-helpies.

After that, I'm trying to use the interceptor from this library and import it like this

import {DuplicateRequestFilter} from '@agilie/angular-helpies/interceptors';

Everything works correctly in dev mode however when I try to build project in production mode I get an error

ERROR in ./src/app/app.module.ngfactory.js
Module not found: Error: Can't resolve 'interceptors' in '/Users/sergey/libs/test-helpies/src/app'
resolve 'interceptors' in '/Users/sergey/libs/test-helpies/src/app'
  Parsed request is a module
  using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./src/app)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in /Users/sergey/libs/test-helpies/
        using description file: /Users/sergey/libs/test-helpies/package.json (relative path: .)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./interceptors)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/interceptors.js doesn't exist
            as directory
              /Users/sergey/libs/test-helpies/interceptors doesn't exist
      /Users/sergey/libs/test-helpies/src/app/node_modules doesn't exist or is not a directory
      /Users/sergey/libs/test-helpies/src/node_modules doesn't exist or is not a directory
      /Users/sergey/libs/node_modules doesn't exist or is not a directory
      /Users/sergey/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /Users/sergey/libs/test-helpies/node_modules
        using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
          using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./node_modules/interceptors)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.tsx doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/sergey/libs/test-helpies/node_modules/interceptors.js doesn't exist
            as directory
              /Users/sergey/libs/test-helpies/node_modules/interceptors doesn't exist
[/Users/sergey/libs/test-helpies/interceptors]
[/Users/sergey/libs/test-helpies/interceptors.ts]
[/Users/sergey/libs/test-helpies/interceptors.tsx]
[/Users/sergey/libs/test-helpies/interceptors.mjs]
[/Users/sergey/libs/test-helpies/interceptors.js]
[/Users/sergey/libs/test-helpies/src/app/node_modules]
[/Users/sergey/libs/test-helpies/src/node_modules]
[/Users/sergey/libs/node_modules]
[/Users/sergey/node_modules]
[/Users/node_modules]
[/node_modules]
[/Users/sergey/libs/test-helpies/node_modules/interceptors]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.ts]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.tsx]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.mjs]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.js]
 @ ./src/app/app.module.ngfactory.js 14:0-35 15:1945-1970
 @ ./src/main.ts
 @ multi ./src/main.ts

You can reproduce the error using this demo project: https://github.com/SergeyMell/test-helpies

What am I doing wrong? Any help is appreciated.

Update

Answers in Field 'browser' doesn't contain a valid alias configuration seem to provide solutions how to fix actually broken package in consuming app. In my case as I understand I have some issues in building or distributing my package so this should be fixed.

2 Answers

Finally, I managed to overcome the issue. The problem was in package namings. As long as my root package is names as @agilie/angular-helpies the children packages should be named as @agilie/angular-helpies/interceptors and @agilie/angular-helpies/decorators but not simply interceptors or decorators.

This looks very strange because if I try to generate the lib with such name I get an error Project name "@agilie/angular-helpies/<some name here>" is invalid. Also when I rename the packages manually I get a warning that String violates the pattern. However, it works.

Possibly this could be fixed in some other manner (npm links or smth different) but I found this one solution. Actually Angular has similar namings if you check https://github.com/angular/angular/blob/master/packages/common/http/package.json for example

Related