Skip to content

Node < 20 compatibility issue: ReferenceError: File is not defined in v10.0.0 #73

Closed
@manisatwick

Description

@manisatwick

Node.js compatibility issue: ReferenceError: File is not defined in v10.0.0

Bug Description
The json-schema-library v10.0.0 throws a ReferenceError: File is not defined when running in Node.js environments. The library appears to be trying to access browser-specific APIs (File, FileList, FileReader) that are not available in Node.js.

Environment

  • json-schema-library version: 10.0.0
  • Node.js version: v18.19.0
  • npm version: 10.2.3
  • Operating System: Windows/Linux/macOS
  • Module type: ES modules ("type": "module" in package.json)

Steps to Reproduce

  1. Install json-schema-library v10.0.0 in a Node.js project with ES modules
  2. Try to use compileSchema and call getData() on the compiled schema

Minimal reproduction case:

import pkg from 'json-schema-library';
const { compileSchema } = pkg;

const simpleSchema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' }
  }
};

const compiledSchema = compileSchema(simpleSchema);
const data = compiledSchema.getData({}); // Throws: ReferenceError: File is not defined

Expected Behavior

The library should work seamlessly in Node.js environments without requiring browser-specific polyfills.

Actual Behavior

ReferenceError: File is not defined
    at Object.getData (node_modules/json-schema-library/dist/jsonSchemaLibrary.js:1:111100)
    at Object.getData (node_modules/json-schema-library/dist/jsonSchemaLibrary.js:1:38206)

Current Workaround

Adding Node.js polyfills for browser APIs:

if (typeof global !== 'undefined') {
  if (!global.File) {
    (global as any).File = class File {
      constructor(public name: string) {}
    };
  }
  
  if (!global.FileList) {
    (global as any).FileList = class FileList {
      length = 0;
      item() { return null; }
    };
  }
  
  if (!global.FileReader) {
    (global as any).FileReader = class FileReader {
      readAsText() {}
      addEventListener() {}
    };
  }
}

Analysis

The issue appears to be that the library is designed for browser environments and tries to access File, FileList, and FileReader objects that don't exist in Node.js. For a schema validation/compilation library, these browser-specific APIs shouldn't be necessary for core functionality.

Expected Fix

The library should either:

  1. Detect the environment and avoid using browser-specific APIs in Node.js
  2. Provide built-in polyfills for Node.js environments
  3. Make the browser-specific functionality optional/conditional

Additional Context

This issue prevents the library from being used in server-side applications, CLI tools, and other Node.js environments without manual polyfills. Since schema validation and data generation are common server-side tasks, Node.js compatibility should be a priority.

Package.json Configuration

{
  "type": "module",
  "dependencies": {
    "json-schema-library": "^10.0.0"
  }
}

Impact

  • Breaks Node.js compatibility for a library that should work universally
  • Requires manual workarounds for server-side usage
  • Affects CLI tools, build scripts, and backend applications using the library

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions