ng-packagr: Module not found: Error: Can't resolve ' commonjs-proxy:../..

Type of Issue

[x ] Bug Report
[ ] Feature Request

Description

Unsure if this is an angular-cli issue or ng-packagr.

Have an angular 6 application containing a library project. When I build using ng-packagr directly the resulting npm package can be consumed from another Angular 6 application. When I build using the angular-cli the resulting package cannot be consumed from another Angular 6 application and get multiple errors as follows when trying to build the consuming Angular 6 app:

ERROR in ./node_modules/c-lib/fesm5/c-lib.js Module not found: Error: Can’t resolve ’ commonjs-proxy:…/…/a-service/a.service’ in ‘C:\data\sandbox\angular\example-ng6-lib-app\node_modules\c-lib\fesm5’

How To Reproduce

  1. Given the following configuration in angular.json, build the library using the angular cli and then package using npm:
ng build --prod c-lib
cd dist/c-lib && npm pack

angular.json:

"architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/c-lib/tsconfig.lib.json",
            "project": "projects/c-lib/ng-package.json"
          },
          "configurations": {
            "production": {
              "project": "projects/c-lib/ng-package.prod.json"
            }
          }
        },
  1. Install the library into another angular 6 application and attempt to consume a component from the installed library

  2. Build the consuming application

Expected Behaviour

Expect the consuming application to build successfully and consumed component to function correctly.

This expected behaviour is achieved when using ng-packagr directly replacing step 1 above with the following:

ng-packagr -p ./projects/c-lib/ng-package.prod.json
cd dist/c-lib && npm pack

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr:            4.0.0-rc.0
@angular/compiler:     6.0.0
rollup:                0.59.4
tsickle:               0.27.5
typescript:            2.7.2

Please include all version numbers that might be relevant, e.g. third-party libraries

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 17 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@AlexanderKozhevin, for thanks the reproduction. And as always in the open source community a good reproduction always gets things solved fast.

What I noticed is that the module format used in esm5 and fesm5 is commonjs. This is due that in tsconfig.lib.json you don’t have a module specified. Please add the following.

"compilerOptions": {
    "module": "es2015",
     ...
},

@alan-agius4 https://github.com/AlexanderKozhevin/angular6-library-test Here we are. Created project from scratch, added library, got an error. Please, let me know if you know how to fix that.

Actually what matters is the module, as there is no such module as es2017 which means it will fallback to commonjs

See: https://github.com/Microsoft/TypeScript/blob/25462beeb7468acbcf411a9ad5092a24bf56dd8a/src/compiler/types.ts#L4586-L4594

@alan-agius4 30 min, I’ll make repository.