How to use the outputJsonSync function from fs-extra

Find comprehensive JavaScript fs-extra.outputJsonSync code examples handpicked from public code repositorys.

The fs-extra.outputJsonSync method writes JSON data to a file on disk.

95
96
97
98
99
100
101
102
103
104
  }
}).then((result) => {
  const projectConfig = fs.readJsonSync(path.join(projectDir, projectConfigName))
  projectConfig.name = projectName
  projectConfig.version = `1.0.0`;
  fs.outputJsonSync(path.join(projectDir, 'package.json'), projectConfig, { spaces: 4, EOL: '\r\n' })

  spinner.succeed()
  console.info('Created successfully');
}).catch((err) => {
fork icon0
star icon0
watch icon1

+ 3 other calls in file

36
37
38
39
40
41
42
43
44
45
   /**
    * write critical initialize error into a single file. workerpool has a problem with emit
    * of this errors - all of this errors emits with message "Worker terminated unexpectedly"
    * without any message about what's exactly happened inside worker that cause process exit.
    */
   fs.outputJsonSync(path.join(process.env.logsPath, `worker/worker-critical-error-${process.pid}.json`), error);
   process.exit(1);
});

/**
fork icon0
star icon0
watch icon0

+ 3 other calls in file

How does fs-extra.outputJsonSync work?

The fs-extra.outputJsonSync method is a utility function in the fs-extra module for Node.js that writes JSON data to a file on disk in a synchronous manner. The function takes two arguments: the first argument is the file path to write the JSON data to, and the second argument is the data to be written. The data can be any JSON-serializable object, such as a string, number, array, or object. The function writes the JSON data to the file in a formatted manner with indentation and newlines. If the file already exists, the function overwrites the existing file. If the file does not exist, the function creates a new file with the specified file path. If an error occurs during the write operation, such as a permissions error or an invalid file path, the function throws an error. For example, the following code uses fs-extra.outputJsonSync to write JSON data to a file: javascript Copy code {{{{{{{ const fs = require('fs-extra'); const data = { name: 'John', age: 30, hobbies: ['reading', 'gaming', 'coding'] }; fs.outputJsonSync('data.json', data); In this example, we have an object data that represents a person with a name, age, and hobbies. We use fs-extra.outputJsonSync to write this data to a file called data.json. The resulting data.json file contains the formatted JSON data: json Copy code {{{{{{{ class="!whitespace-pre hljs language-json">{ "name": "John", "age": 30, "hobbies": [ "reading", "gaming", "coding" ] } Overall, fs-extra.outputJsonSync provides a simple way to write JSON data to a file on disk in a synchronous manner.

54
55
56
57
58
59
60
61
62
63
64
65
      }
      logger.info(`Hook with name "${hookName}" isn't found! Check your hooks API for this one.`);
   }


   saveExecutedHooks(logFolder) {
      fs.outputJsonSync(path.join(logFolder, 'executed-hooks.json'), this.executedHooks);
   }
}


module.exports = {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

423
424
425
426
427
428
429
430
431
432
433
434
      } else {
         result.push(toPosix(module.path));
      }
   });


   fs.outputJsonSync(path.join(SOURCE_ROOT, 'directories_to_watch.json'), result);


   return result;
}

fork icon0
star icon0
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const fs = require("fs-extra");

const data = {
  name: "Alice",
  age: 25,
  hobbies: ["drawing", "reading", "swimming"],
};

fs.outputJsonSync("data.json", data);

In this example, we have an object data that represents a person with a name, age, and hobbies. We use fs-extra.outputJsonSync to write this data to a file called data.json. The resulting data.json file contains the formatted JSON data: json Copy code

245
246
247
248
249
250
251
252
253
254
let reportName = 'builder_report';
if (process.argv.includes('runTypescript')) {
   reportName += '_tsc';
}
const reportFilePath = path.join(resultPath, `${reportName}.json`);
fs.outputJsonSync(reportFilePath, { messages });
let resultMessage = 'build was completed ';
if (logsLevels.warnings || logsLevels.errors) {
   resultMessage += `with warnings or errors. See ${reportFilePath} for additional info!`;
} else {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

69
70
71
72
73
74
75
76
77
78
    * 2) log folder is for correct storage of modules_stats in build artifacts
    * and for further processing in wasaby-cli
    */
   directories.forEach((currentDirectory) => {
      const reportFilePath = path.join(currentDirectory, 'modules_stats.json');
      fs.outputJsonSync(reportFilePath, report);
   });
}

registerFiledModule(moduleInfo) {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

28
29
30
31
32
33
34
35
36
37
38
39
];


fs.ensureDirSync(buildPath);


for (let contract in output) {
  fs.outputJsonSync(
    path.resolve(buildPath, contract.replace(":", "") + ".json"),
    output[contract]
  );
}
fork icon0
star icon0
watch icon0

185
186
187
188
189
190
191
192
193
194
            // update latest_version.txt
            fs.outputFileSync(path.resolve(dest, 'latest_version.txt'), uuid);
        }
    } else {
        // update version.json
        fs.outputJsonSync(path.resolve(dest, 'version.json'), newVersionContent);
        // update latest_version.txt
        fs.outputFileSync(path.resolve(dest, 'latest_version.txt'), uuid);
    }
}
fork icon0
star icon0
watch icon0

+ 2 other calls in file

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)