Skip to content

saveDraftData with validate: true throws 'glob is not a function' error in Node.js scripts #913

@jeremydw

Description

@jeremydw

Description

When using RootCMSClient.saveDraftData() with validate: true in a Node.js script, an error is thrown:

TypeError: (intermediate value).glob is not a function
    at file:///node_modules/@blinkk/root-cms/dist/chunk-RNDSZKAW.js:2:34
    at RootCMSClient.getCollection
    at RootCMSClient.saveDraftData

Root Cause

The issue is in core/project.ts which uses import.meta.glob():

var SCHEMA_MODULES = import.meta.glob(
  [
    "/**/*.schema.ts",
    "!/appengine/**/*.schema.ts",
    ...
  ],
  { eager: true }
);

import.meta.glob is a Vite-specific feature that doesn't exist in standard Node.js. When running scripts outside of Vite (e.g., migration scripts, CLI tools), this fails.

Steps to Reproduce

  1. Create a Node.js script that uses RootCMSClient
  2. Call saveDraftData with validate: true:
import {RootCMSClient} from '@blinkk/root-cms';
import {loadRootConfig} from '@blinkk/root/node';

const rootConfig = await loadRootConfig(rootDir, {command: 'dev'});
const cmsClient = new RootCMSClient(rootConfig);

await cmsClient.saveDraftData('Pages/research', fields, {
  modifiedBy: 'script',
  validate: true,  // This causes the error
});

Recommended Fix

Option 1: Runtime detection with fallback

// In core/project.ts
let SCHEMA_MODULES = {};

if (typeof import.meta.glob === 'function') {
  // Vite environment
  SCHEMA_MODULES = import.meta.glob([...], { eager: true });
} else {
  // Node.js fallback - use fs to find and import schemas
  // This could be initialized lazily when getProjectSchemas() is called
}

Option 2: Separate entry points

Create separate bundles for browser/Vite vs Node.js environments, with the Node.js version using fs.readdirSync + dynamic import() instead of import.meta.glob.

Option 3: Pass schemas via config

Allow the root config to include pre-loaded schemas when running in Node.js:

const rootConfig = await loadRootConfig(rootDir, {
  command: 'dev',
  preloadSchemas: true  // Would load schemas using fs
});

Environment

  • root-cms version: 2.5.1
  • Node.js version: v22.x

Workaround

Using saveDraftData without validate: true works. Using setRawDoc also works.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions