angular-quickstart-lib icon indicating copy to clipboard operation
angular-quickstart-lib copied to clipboard

Demo app fails when using util package in a library component

Open Bradleycorn opened this issue 7 years ago • 4 comments

Steps to reproduce:

  1. clone the repo, and npm install
  2. npm install --save-dev util
  3. update the /src/lib/src/component/lib.component.ts file with:
import {isNullOrUndefined} from 'util';

and give the component a constructor as follows:

  constructor() {
    let bool = (isNullOrUndefined(name));
  }
  1. run the demo app: npm start

The app doesn't load, and the console gives errors about not being able to find util.

Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3002/util
	Error: XHR error (404 Not Found) loading http://localhost:3002/util
	    at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:3002/node_modules/zone.js/dist/zone.js:1075:39)
	    at ZoneDelegate.invokeTask (http://localhost:3002/node_modules/zone.js/dist/zone.js:424:31)
	    at Zone.runTask (http://localhost:3002/node_modules/zone.js/dist/zone.js:191:47)
	    at ZoneTask.invokeTask [as invoke] (http://localhost:3002/node_modules/zone.js/dist/zone.js:498:34)
	    at invokeTask (http://localhost:3002/node_modules/zone.js/dist/zone.js:1370:14)
	    at XMLHttpRequest.globalZoneAwareCallback (http://localhost:3002/node_modules/zone.js/dist/zone.js:1388:17)
	Error loading http://localhost:3002/util as "util" from http://localhost:3002/quickstart-lib/src/component/lib.component.js
	    at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:3002/node_modules/zone.js/dist/zone.js:1075:39)
	    at ZoneDelegate.invokeTask (http://localhost:3002/node_modules/zone.js/dist/zone.js:424:31)
	    at Zone.runTask (http://localhost:3002/node_modules/zone.js/dist/zone.js:191:47)
	    at ZoneTask.invokeTask [as invoke] (http://localhost:3002/node_modules/zone.js/dist/zone.js:498:34)
	    at invokeTask (http://localhost:3002/node_modules/zone.js/dist/zone.js:1370:14)
	    at XMLHttpRequest.globalZoneAwareCallback (http://localhost:3002/node_modules/zone.js/dist/zone.js:1388:17)
	Error loading http://localhost:3002/util as "util" from http://localhost:3002/quickstart-lib/src/component/lib.component.js

How can we use 'util' with the demo project?

Bradleycorn avatar Aug 13 '17 00:08 Bradleycorn

You need to add the entry of 'util' in the systemjs.config.js of the demo app.

ttma1046 avatar Aug 13 '17 02:08 ttma1046

Like so?

    map: {
      'util': 'node_modules/util',

       // ... additional maps ...
     }

I tried that ... it didn't work. also tried setting the path to node_modules/util/util.js, still didn't work. Also tried adding an entry to packages for util, still didn't work.

Bradleycorn avatar Aug 13 '17 03:08 Bradleycorn

Hi,

Could you post the error after you adding 'util': 'node_modules/util/util.js' entry into the 'map' section of the systemjs.config.js inside of the demo app?

Btw since 'util' is something your lib component directly depended on, it should be in the 'dependencies' of the package.json not 'devdependencies'.

I guess 'npm install util --save' instead of 'npm install util --save-dev'.

ttma1046 avatar Aug 26 '17 08:08 ttma1046

@Bradleycorn I faced the same problem and what worked for me was to add the dependency under the "paths" key in systemjs.config.js, rather than the "map" key:

    paths: {
      // ...
      'util': 'node_modules/util',
      // ... others
    },

mlwilkerson avatar Dec 22 '17 15:12 mlwilkerson