question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Library including CommonJS dependency fails in Angular 13

See original GitHub issue

Type of Issue

[ X ] Bug Report
[ ] Feature Request

Description

We are upgrading our application that uses several libraries to Angular 13. One of the libraries depends on sockjs-client. This library now fails to load the sockjs-client module correctly after the upgrade.

How To Reproduce

Here’s a sample application to show the issue: https://github.com/iamthechad/ng-lib-test

Build the library with npx ng build sample-lib, then run the application with npm start.

If the application component uses the sockjs-client module directly it works correctly. However, if the sockjs-client usage is in the library and exposed through a service, this error is shown in the console:

TypeError: (sockjs_client__WEBPACK_IMPORTED_MODULE_0___namespace_cache || (intermediate value)(intermediate value)) is not a constructor
    at TestService.createSocket (main.js:151)
    at AppComponent.ngOnInit (main.js:28)
    at callHook (vendor.js:23142)
    at callHooks (vendor.js:23111)
    at executeInitAndCheckHooks (vendor.js:23062)
    at refreshView (vendor.js:30100)
    at renderComponentOrTemplate (vendor.js:30199)
    at tickRootContext (vendor.js:31430)
    at detectChangesInRootView (vendor.js:31455)
    at RootViewRef.detectChanges (vendor.js:42027)

This behavior is not limited to sockjs-client and happens with other CommonJS modules.

Expected Behaviour

The sockjs-client module should be resolved correctly when it is used within a library.

Version Information

ng-packagr: 13.1.2
@angular/compiler: 13.1.1
rollup: 2.63.0
typescript: 4.5.4
node: 14.17.6
npm: 6.14.15
sockjs-client: 1.5.2

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:21 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gmixocommented, Sep 28, 2022

for me, in case of use moment.js, helped this approach import moment in library code this way

import moment from 'moment/moment'; 

while in project, import looks like this

import * as moment from 'moment/moment';
2reactions
alan-agius4commented, Jan 17, 2022

You also need to change the following import https://github.com/iamthechad/ng-lib-test/blob/5a935d43245cb1d97707331620a75ad6d4e7a1b8/projects/sample-lib/src/service/test.service.ts#L2 to a default import instead of a namespace import.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 13 in Depth - Sébastien Dubois
The benefit is that it both the application and its libraries rely on a single version of Angular, removing potential compatibility issues.
Read more >
Include third party dependency in custom angular library
This library use of a dependency called memoizee, where we include the library and its respective @types in the peerDependencies section of the ......
Read more >
The Best Way To Architect Your Angular Libraries
CommonJS Dependencies. They wont be tree shaked without using of sub entries and will end up in your main.js, even if you do...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
This is for the convenience of your CJS users. This also ensures that your library can work in older versions of Node. (If...
Read more >
Angular >=13 | jest-preset-angular - GitHub Pages
transform is updated to include .mjs extension to transform to CommonJS codes. Migration steps from Angular < 13​. Upgrade the project to Angular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found