Gathering detailed insights and metrics for jsonfile-updater
Gathering detailed insights and metrics for jsonfile-updater
npm install jsonfile-updater
Typescript
Module System
Min. Node Version
Node Version
NPM Version
79.2
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
1,166,005
Last Day
24
Last Week
24
Last Month
7,406
Last Year
113,244
6 Stars
17 Commits
2 Forks
2 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
3.1.0
Package Id
jsonfile-updater@3.1.0
Size
9.46 kB
NPM Version
5.3.0
Node Version
8.2.1
Publised On
28 Sept 2017
Cumulative downloads
Total Downloads
Last day
0%
24
Compared to previous day
Last week
-98.6%
24
Compared to previous week
Last month
25.6%
7,406
Compared to previous month
Last year
-18.6%
113,244
Compared to previous year
1
Node module for programmatically updating package.json
and other .json
files.
NOTE: This package does not create JSON files, it can only edit existing valid JSON files. A file with just {}
is all you need to get started.
jsonfile-updater
enforces strict typing. Meaning, once a property of a certain type is added in the JSON file, it can be
overwritten or set a new value with the same type only.
Supported types are: boolean, string, number, array, and object. Functions are not supported.
1var updater = require('jsonfile-updater')
Further examples will be referring to the updater
object created in the code above.
If you want to try out the examples, include the following code in your file.
1var fs = require('fs') 2function getParsedPackage() { 3 return JSON.parse(fs.readFileSync('./settings.json')) 4}
The jsonfile-updater
methods support both callback and promise. If you omit the callback parameter,
they return a promise, which can greatly simplify the code when used with async-await.
add(property, value[, callback])
Using the add()
instance method, you can add new properties. If you try to add a property that aleady exists, the module
will return an error. If you want to overwrite an existing property use set()
or append()
.
Adding a string-type property:
Callback
1updater('./settings.json').add('time', 'now', function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.time === 'now') // true 5})
Promise
1updater('./settings.json').add('time', 'now') 2.then(() => { 3 var pkg = getParsedPackage() 4 console.log(pkg.time === 'now') // true 5}, reason => console.log(reason))
Adding an array-type property:
1updater('./settings.json').add('tags', ['nodejs', 'javascript'], function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.tags) 5})
Adding an object-type property:
1updater('./settings.json').add('dependencies', { a: '1.2.1', b: '2.0.0'}, function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.dependencies) 5})
You can add a sub-property using the dot notation:
1updater('./settings.json').add('author.age', 100, function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.author.age) // 100 5})
There are two methods for updating existing properties: set()
for overwriting an existing value, append()
for
adding additional data to an existing value.
set(property, value[, callback])
Using the set()
method, you can overwrite existing properties. If you try to update a property does not exist,
the module will return an error. The new value should be the same as the old value's data type.
Callback
1updater('./settings.json').set('license', 'FREE', function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.license) 5})
Promise
1updater('./settings.json').set('license', 'FREE') 2.then(() => { 3 var pkg = getParsedPackage() 4 console.log(pkg.license) 5}, reason => console.log(reason))
1updater('./settings.json').set('tags', 'cool', function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.tags.includes('cool')) // true 5})
1updater('./settings.json').update('author', { 'username': 'hacksparrow' }, function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.author) 5})
You can target a sub-property using the dot notation:
1updater('./settings.json').update('author.age', 200, function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.author.age) // 200 5})
append(property, value[, preserve][, callback])
Using the append()
method, you can append items to an existing value. If you try to update a property does not exist,
the module will return an error. append()
does not work for all data types and work differently on different data types.
When an array is append to an array, the "appendee" will be concatened to the "appender". To preserve the array of the
"appendee", and push it to the "appender", specify the preserve
option when calling append
.
Appending a string to an array:
Callback
1updater('./settings.json').append('tags', 'cool', function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.tags) 5})
Promise
1updater('./settings.json').append('tags', 'cool') 2.then(() => { 3 var pkg = getParsedPackage() 4 console.log(pkg.tags) 5}, reason => console.log(reason))
Appending a preserved array to an array:
1updater('./settings.json').append('tags', ['cool', 'new'], true, function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.tags) 5})
For more examples, refer /test/test.js.
delete(property|[properties ...][, callback])
Using the delete()
method, you can delete existing properties. If you try to delete a property that does not exist,
the module will return an error.
FYI: delete()
is also aliased as remove()
.
Deleting a single property:
Callback
1updater('./settings.json').delete('name', function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg) 5})
Promise
1updater('./settings.json').delete('name') 2.then(() => { 3 var pkg = getParsedPackage() 4 console.log(pkg) 5}, reason => console.log(reason))
Deleting multiple properties:
1updater('./settings.json').delete(['name', 'version'], function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg) 5})
You can target a sub-property using the dot notation:
1updater('./settings.json').delete('author.age', function(err) { 2 if (err) return console.log(err) 3 var pkg = getParsedPackage() 4 console.log(pkg.author.age) // undefined 5})
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
no SAST tool detected
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
Score
Last Scanned on 2025-02-03
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