Why is Meteor client crashing due to code that is not being called, but just defined?

So I’m experiencing an interesting behaviour on Meteor server side, where a code definition is not being called but causes a serverside crash.

I have a function called generateTypes located in /imports/api/_utils.ts

import { compileFromFile } from "json-schema-to-typescript";

/**
 * Generates types based on JSON schema
 */
export const generateTypes = async () => {
 const jsonFileLocation = getJsonFileLocation();
 const ts = await compileFromFile(jsonFileLocation); 
   ...
}

When I run meteor run, this code works and generates the expected types based on my JSON Schema defintions, and no error is being thrown in the server side:

$ meteor run

=> Started proxy.                             
=> Started HMR server.                        
=> Started MongoDB.                           
=> Started your app.                          

=> App running at: http://localhost:3000/

However, if I navigate to http://localhost:3000 in my browser, the browser crashes with the error message in the console:

Middleware.js:102 Uncaught TypeError: Cannot convert undefined or null to object
    at Function.assign (<anonymous>)
    at module (Middleware.js:102:17)
    at fileEvaluate (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:388:7)
    at Module.require (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:270:27)
    at require (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:310:21)
    at module (Middleware.js:102:17)
    at fileEvaluate (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:388:7)
    at Module.require (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:270:27)
    at require (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:310:21)
    at module (Middleware.js:102:17)

Now here’s the interesting part: Even if I return out of the function right away (or don’t even call the function at all), the error still occurs!!!

import { compileFromFile } from "json-schema-to-typescript";

/**
 * Generates types based on JSON schema
 */
export const generateTypes = async () => {
 return; // <-- debugging purposes. Even if this function returns right away, the code below still causes the crash!?! 
 const jsonFileLocation = getJsonFileLocation();
 const ts = await compileFromFile(jsonFileLocation);  // <-- this causes the error  (even if this part of the code never gets reached. I'm very confused. ). Commenting this out removes the crash.
   ... 
}

Why is this? Can someone explain how just defining a function that is not even being called is causing this error?

Thanks

Some Modules produce side effects on import.

You can try that yourself with one of your js files that you export from. Just add a line like throw('I crash the app on import.') in the top level.

We use that all the time when we set up our methods and publications. They get set up on import from our entry points even if we don’t export anything from the modules.

When you look at the stack trace, you will notice that the error is not from within compileFromFile from some other stuff that follows Module.require

1 Like