Installations
npm install jsonfile-updater
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=6
Node Version
8.2.1
NPM Version
5.3.0
Score
79.2
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
hacksparrow
Download Statistics
Total Downloads
1,166,005
Last Day
24
Last Week
24
Last Month
7,406
Last Year
113,244
GitHub Statistics
6 Stars
17 Commits
2 Forks
2 Watching
1 Branches
1 Contributors
Bundle Size
3.43 kB
Minified
1.23 kB
Minified + Gzipped
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
1,166,005
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
1
jsonfile-updater
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.
Usage
Instantiation
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.
Adding properties
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})
Updating properties
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.
- Booleans cannot be appended to anything
- A string can be appended only to string or array (pushed)
- A number can be appended only to an array (pushed)
- An array can be appended only to another array (concatenated by default)
- An object can be appended only to an array (pushed) or another object (merged)
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.
Deleting properties
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})
LICENSE
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
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