I tried a ton of checks. This definitely is an anomaly. I have an Angular 2 service that loads a @type
definition (typescript 2) which in turn loads a commmon.js module (visionmedia/debug). Inside the common.js module I have a simple if statement that triggers an error even if the condition is false and code should not be executed. The Angular app uses system.js
to load modules.
Regular Code if (false) { console.log('This code is not executed') }
Normal behavior, nothing happens
Anomaly Code: if (false) { exports.humanize = require('ms'); }
It triggers
Error: zone.js:101 GET http://localhost:8002/ms.js 404 (Not Found)
The error itself is valid. Indeed that script is not to be found. What is deeply wrong is that it should not appear in the first place. It should be blocked by the false if condition. It seems to me that zone.js
somehow parses the the instruction even if the case is false. What can I do to avoid this? I need to check wheter to require one path or another depending if the same script is called on server or on frontend.
Bigger picture inside the CJS module:
// Trying to detect if environment is node.js
// In backend (no zones) everything works as expected
// In frontend, the require('ms') statement is executed event if condition is false.
// I checked manually if process is defined, it's not.
// Event the basic `false` condition also fails to block code.
if (typeof process === 'undefined') {
exports.humanize = require('node_modules/ms/index.js');
console.log('Browser');
} else {
exports.humanize = require('ms'); // If I comment this code works as intended
console.log('Node');
}