Jeanluca Scaljeri Jeanluca Scaljeri - 1 month ago 9
Javascript Question

Use zone.js inside node (not angular)

I would like to use zone.js in my node app.

Although I have found a post in which is described how to do this, I still get

zone
is not defined error

For example. I also found an examples likt this

let zone = require('zone');

zone.run(function () {
zone.inTheZone = true;

setTimeout(function () {
console.log('in the zone: ' + !!zone.inTheZone);
}, 0);
});


Which gives the same error, but I inspected the
zone
object, I noticed that it returns an object like this

{
enable: function() { .. }
}


So I guess that the zone API changed. Any help would be appreciated

Answer Source

There are two things:

  1. require zone.js instead of zone
  2. use Zone.current to get reference to the current zone

This works:

const zone = require('zone.js');

Zone.current.run(function () {
    zone.inTheZone = true;

    setTimeout(function () {
        console.log('in the zone: ' + !!zone.inTheZone);
    }, 0);
});