Skip to content

JSON Module

This page describes how to use ESM-style imports for JSON files and the special $write functionality to persist changes back to the file system. This runtime feature allows importing JSON files as modules, similar to Node.js, while also enabling programmatic updates to the imported JSON data.

Importing a JSON File

To import a JSON file in this CEF runtime, use ESM syntax as you would in Node.js:

ts
import config from './config.json'

Once imported, config holds the JSON data as an object.

WARNING

You cannot import JSON modules from remote URL.

Accessing JSON Properties

You can read properties from the imported JSON object as usual:

ts
console.log(config.x) // Outputs the value of `x` in config.json

Writing JSON Data

since v1.2.0

Pengu runtime allows you to modify properties of the imported JSON object. To persist these changes back to the file system, use the special $write method.

Modify the properties:

ts
config.x = 20 // Modify a property

Call $write to save:

ts
await config.$write()

The $write method will asynchronously write the current state of the config object back to config.json, preserving all modifications.

Released under the MIT License.