Pace Pace - 4 months ago 10
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 =
util.deprecate(Address4.prototype.toGroup6,
'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...

System.import('ip-address');


...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 = {
'ip-address':'vendor/ip-address',
'util':'vendor/util-deprecate'
}

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 =
util(Address4.prototype.toGroup6,
'deprecated: `toV6Group` has been renamed to `toGroup6`');


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

Answer

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.