Gathering detailed insights and metrics for modify-json-file
Gathering detailed insights and metrics for modify-json-file
Gathering detailed insights and metrics for modify-json-file
Gathering detailed insights and metrics for modify-json-file
magicast
Modify a JS/TS file and write back magically just like JSON!
file-assistant
This is a stable and tested file-system package to create, copy, move, merge and overwrite files and folders, to empty folders and to write, modify or append content to the files, according to the given [Array] or JSON structure instructions.
json-file-operator
change and modify jsonFile.
object-json-file
A Tool to read or modify object_json file according to key.
npm install modify-json-file
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
46 Commits
1 Watchers
2 Branches
2 Contributors
Updated on Aug 31, 2023
Latest Version
1.2.2
Package Id
modify-json-file@1.2.2
Unpacked Size
16.13 kB
Size
5.68 kB
File Count
7
NPM Version
6.14.15
Node Version
14.18.1
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Simplest way to modify JSON files
Becaues I got tired of writing read
/write
functions for JSON files, especially when I need to change dozens of files.
Let's start with package.json file:
1// 📁package.json 2{ 3 "name": "package", 4 "main": "index.js", 5 "author": "eldar", 6 "files": [ "build" ], 7 "dependencies": { 8 "type-fest": "*", 9 "fdir": ">=2" 10 } 11}
And this code:
1import { modifyJsonFile } from "modify-json-file"; 2 3// modify package.json in the same dir 4await modifyJsonFile( 5 path.join(__dirname, "package.json"), 6 { 7 name: s => `super ${s}`, 8 main: "build/electron.js", 9 files: undefined, // removing the property 10 dependencies: { 11 "type-fest": "^1.0.0" 12 } 13 } 14)
After running the code, package.json will be:
1{ 2 "name": "super package", 3 "main": "build/electron.js", 4 "author": "eldar", 5 "dependencies": { 6 "type-fest": "^1.0.0" 7 } 8}
As you can see above, modifyJsonFile
only merges only on top level properties. Currently, there is no support for nested property merging.
Note that to simplify docs I won't use path
module here, but you should use it everywhere.
In example above,
modifyPackageJson
should be used to provider typing for you
As always, for more usage examples you can look into test-d/index.test-d.ts
or tests
files.
Remember, that at root level value can be any valid JSON value:
string
,number
,boolean
,null
,object
orarray
.
Be aware of modifying non object JSON files (where root type is not an object). For example:
Our code:
1import { modifyJsonFile } from "modify-json-file"; 2 3// telling that root type is number (in this case it's obligatory) 4await modifyJsonFile<number>("package.json", n => n + 1);
Expected JSON:
1// 📁someNumber.json 25
Actual JSON:
1// 📁someNumber.json 2{ 3 "retries": 5 4}
After running the code above, without any warnings you will get this:
1// 📁someNumber.json 2"[object Object]1"
That's because callback n => n + 1
has transformed n
(object) into string.
Here, despite of the TS type (number), n
is object in runtime, so n + 1
just stringified n
and returned [object Object]1
.
Then this module just stringified the string to store output as valid JSON string in file.
Remember, this module doesn't do any type checking in runtime, you need to use typeof
in callback for checking root types or schema validators (like ajv) for objects.
By default, it will preserve tab size (thanks to detect-indent), but you can control this by overriding tabSize
option. For example, we can use it to just format the file:
1import { modifyJsonFile } from "modify-json-file"; 2 3// this will format file to use \t (hard tabs) 4await modifyJsonFile("someFile.json", {}, { tabSize: "hard" });
Pass { removeJsonc: true }
option to enable processing jsonc
files.
WARNING, this option will remove comments and trailing commas forever!
This is temporary limitation and the option will be renamed to jsonc
once limitation is removed.
modifyTsConfigJsonFile
has this option enabled by default
Docs:
Helper exports section
Extend package.json typings
Examples with immer
Make usage more clear
Fix auto generated docs
Describe all possible usage cases
Give a hint, that it doesn't perform schema checking again actual file contents when type is passed into generic function modifyJsonFile
Performance investigation (issues welcome)
Strip bom option
Fix double tsc compilation (and test actual build/)
transformer for paths (most likely babel plugin):
1await fs.promises.readFile(./package.json, "utf8");
Into this:
1await fs.promises.readFile(path.join(__dirname, "package.json"), "utf8");
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
15 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More