angular-quickstart-lib
angular-quickstart-lib copied to clipboard
Demo app fails when using util package in a library component
Steps to reproduce:
- clone the repo, and
npm install
-
npm install --save-dev util
- 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));
}
- 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?
You need to add the entry of 'util' in the systemjs.config.js of the demo app.
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.
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'.
@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
},