Pace Pace - 1 year ago 83
Javascript Question

"Namespacing" an import in systemJS

I want to use a library ip-address with SystemJS (note, this question may look similar but it is a different problem I ran into trying while trying to accomplish this task).

The library ip-address depends on util-deprecate. It imports it as follows:

var util = require('util');

And then uses it as follows:

Address4.prototype.toV6Group =
'deprecated: `toV6Group` has been renamed to `toGroup6`');

When I import ip-address in a node project as...

var ipAddress = require('ip-address');

...then I don't get any problems.

When I import ip-address in a SystemJS project...


...then I get an error:

util.deprecate is not a function

How can I configure SystemJS to perform this import? Currently I am configuring it as so...

const map: any = {

const packages: any = {
'ip-address': {main:'ip-address.js'},
'util': {main: 'browser'}

Just to save a lookup, the util-deprecate's browser.js file is here, it is exporting the deprecate function directly.

Note, I can get this to work if I modify the ip-address module so that all calls are of the form:

Address4.prototype.toV6Group =
'deprecated: `toV6Group` has been renamed to `toGroup6`');

I'd rather not modify a 3rd-party library if I can avoid it however.

Answer Source

Ok, it turns out the issue was that I thought the ip-address module was using util-deprecate. It turns out that the way the ip-address module was importing util...

var util = require('util');

It was not importing util-deprecate but importing Node's built in package util. So, in order for ip-address to truly use util-deprecate a change will have to be made to the ip-address module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download