Use JSON paths to set or remove values in TOML strings, while keeping comments and formatting.
npm i toml-json-path-replacerimport { tomlJSONPathReplacer } from 'toml-json-path-replacer';
// or: const { tomlJSONPathReplacer } = require('toml-json-path-replacer');
const toml = `
name = "my-worker" # worker name
`;
const updatedTOML = tomlJSONPathReplacer(
toml, // the TOML string
['name'], // the JSON path to set
'my-updated-worker', // the new value, or undefined to remove the path
);
// name = "my-updated-worker" # worker nametomlJSONPathReplacer returns a new string. It does not change its arguments.
const toml = `
workers_dev = false
route = { pattern = "example.org/*", zone_name = "example.org" } # my route
kv_namespaces = [
{ binding = "<MY_NAMESPACE>", id = "<KV_ID>" } # my kv
]
[limits]
cpu_ms = 100
[env.staging]
# Overrides above
route = { pattern = "staging.example.org/*", zone_name = "example.org" }
`;
tomlJSONPathReplacer(toml, ['workers_dev'], true);
// workers_dev = true
tomlJSONPathReplacer(toml, ['route', 'pattern'], 'api.example.org/*');
// route = { pattern = "api.example.org/*", zone_name = "example.org" } # my route
tomlJSONPathReplacer(toml, ['limits', 'cpu_ms'], 50);
// [limits]
// cpu_ms = 50
tomlJSONPathReplacer(toml, ['kv_namespaces', 0, 'id'], 'new-id');
// kv_namespaces = [
// { binding = "<MY_NAMESPACE>", id = "new-id" } # my kv
// ]
tomlJSONPathReplacer(toml, ['env', 'staging', 'route', 'pattern'], 'api.staging.example.org/*');
// [env.staging]
// # Overrides above
// route = { pattern = "api.staging.example.org/*", zone_name = "example.org" }
tomlJSONPathReplacer(toml, ['env', 'staging', 'route'], {
pattern: 'api.staging.example.com/*',
zone_name: 'api.staging.example.com',
});
// [env.staging]
// # Overrides above
// route = { pattern = "api.staging.example.com/*", zone_name = "api.staging.example.com" }
tomlJSONPathReplacer(toml, ['env', 'production'], {
route: {
pattern: 'example.com/*',
zone_name: 'example.com',
},
});
// adds this to the end of the file:
// [env.production]
// route = { pattern = "example.com/*", zone_name = "example.com" }
tomlJSONPathReplacer(toml, ['env', 'staging'], undefined);
// removes the [env.staging] table- Paths. Each path segment is a key or an array index. A number, or a digit string such as
"0", is an index when it points into an array. On an existing table it is a plain key. A number that is not a non-negative integer, such as-1or1.5, throws. An empty path throws. - Missing paths. A missing path is created. New tables go to the end of the file. New tables inside a table array element go after that element's sub-tables. New top-level values go to the top of the file, where no table header can claim them. They go after any
#:directive lines, such as#:schema. - Arrays. You can replace any element or append one at the end. Skipping past the end throws
Cannot skip array elements when inserting. - Type changes. Setting a new type replaces the old value, its sub-tables included. A table array element that is set to a non-table turns the table array into an inline array. The other elements keep their values as written.
- Removals. Pass
undefinedto remove a path and everything below it. A[table]section stays in place, empty, when you remove its last key. A table that only dotted keys define, such asaina.b = 1, goes away with its last key. So does a table array with its last element. Removing a path that does not exist returns the input unchanged. - Formatting. Comments and layout outside the changed value stay as they are. This includes line endings and a byte order mark. Edits inside arrays and inline tables keep the text and comments of the other items. A replaced table keeps its header line. A new value is written in a standard form, so
0x10set to16becomes16. - Rewrites. A change that the current form cannot express rewrites the whole value. One example is a table array that becomes an inline array. Comments inside the rewritten value are lost.
- Values. Strings, numbers, booleans,
bigint,Date, arrays, and plain objects are supported. Object properties set toundefinedare skipped. These throw:null,undefinedinside an array (holes in a sparse array included), abigintoutside the 64-bit range, an invalidDateor one with a year outside 0 to 9999, a value that contains itself, and any other object, such as aMapor a class instance. - TOML versions. The input can use TOML 1.0 or TOML 1.1, such as inline tables over several lines.
- Errors. Invalid input TOML throws a parse error. The function never returns invalid TOML: if an update would produce it, the function throws instead.
- Node.js 20.19 or later, 22.13 or later, and 24 or later.
- Cloudflare Workers.
- Browsers, through a bundler or a CDN that serves ES modules.
The package ships ES modules and CommonJS, with TypeScript types for both.