How to use the writeJSON function from fs-extra

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

fs-extra.writeJSON is a method in the fs-extra library that allows you to write a JSON object to a file on disk.

159
160
161
162
163
164
165
166
167
168

const packageJson = await fs.readJSON(sysPath.join(root, "package.json"));
const { scripts, ...otherConfigs } = packageJson;
scripts.prettier = `npx prettier --list-different \"./src/**/*.{ts,tsx,md}\" \"./app/**/*.{ts,tsx,md}\""`;
const newPackageJson = { ...otherConfigs, scripts };
await fs.writeJSON(sysPath.join(root, "package.json"), newPackageJson, {
  spaces: 2,
});

const installCmd = getInstallCmd();
fork icon5
star icon36
watch icon0

+ 3 other calls in file

14
15
16
17
18
19
20
21
22
} catch {
  // Ignore
}

try {
  await fs.writeJSON(dest, { version: pkg.version });
} catch {
  // Ignore
}
fork icon0
star icon1
watch icon1

+ 2 other calls in file

How does fs-extra.writeJSON work?

fs-extra.writeJSON is a method in the fs-extra library that allows you to write a JSON object to a file on disk. It takes two parameters: the file path and the JSON object to write. When you call fs-extra.writeJSON, it creates a new file or overwrites the contents of an existing file with the specified JSON object. The JSON object is converted to a string and written to the file using the fs-extra.writeFile method. By default, fs-extra.writeJSON will use two-space indentation when formatting the JSON output. However, you can customize this behavior by passing an optional third parameter, which is an options object that allows you to specify the number of spaces to use for indentation and other formatting options. If an error occurs while writing the JSON object to the file, fs-extra.writeJSON will throw an exception. You can handle this exception by wrapping your call to fs-extra.writeJSON in a try-catch block or using a promise-based approach.

182
183
184
185
186
187
188
189
190
191
192
193
    _.set('strapi.template', templatePackageInfo.name, mergedConfig);
  }


  // Save the merged config as the new package.json
  const packageJSONPath = path.join(rootPath, 'package.json');
  await fse.writeJSON(packageJSONPath, mergedConfig, { spaces: 2 });
}


// Merge all allowed files and directories
async function mergeFilesAndDirectories(rootPath, templatePath) {
fork icon0
star icon0
watch icon1

+ 6 other calls in file

120
121
122
123
124
125
126
127
128
129
130


async function removeExtensionDependencies(done) {
  const packageJson = await readJSON("package.json");
  if (packageJson.extensionDependencies) {
    packageJson.extensionDependencies = undefined;
    await writeJSON("package.json", packageJson, { spaces: 2});
  }
  done();
}

fork icon0
star icon0
watch icon1

+ 13 other calls in file

Ai Example

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

const data = {
  name: "John Doe",
  age: 30,
  email: "john.doe@example.com",
};

// Write the data to a JSON file named 'data.json' in the current directory
fs.writeJSON("data.json", data)
  .then(() => console.log("Data written to file"))
  .catch((err) => console.error(err));

This example creates an object called data with properties for a person's name, age, and email. The fs-extra.writeJSON method is then used to write this object to a file named data.json in the current directory. The method returns a promise, so the example uses .then() and .catch() to handle success and error cases respectively. If the file is successfully written, the message "Data written to file" will be logged to the console. If an error occurs, the error object will be logged to the console.

function icon

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