A JSON file-writing package designed for NPM initializers and places where Node needs to write static files.
install: npm i --D @afoot/write-json
import: import writeJson from '@afoot/write-json'
write-json takes the following params:
-
filename |
string
name and extension of the file to be written -
json |
Object
any valid JSON statement. Ideally object literals or arrays -
config |
Object
configuration object (see below)
{
errorCallback: null, // Calls if file write fails
willFormat: true, // Option to disable tab formatting
root: `(current working directory)`, // File path to write
writeOptions: {flag:'w'} // Node options object for `writeFile`
}
const obj = { name: 'test', version: '0.0.1' };
const filename = 'package.json';
writeJson(filename, obj);