How to use the writeJSONSync function from fs-extra

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

fs-extra.writeJSONSync is a method that writes JSON data synchronously to a file.

93
94
95
96
97
98
99
100
101
102
let pkgJson = fs.readJSONSync(path.join('ui', 'package.json'));
// Add dependencies object if none is found in the package.json because generated
// SvelteKit projects do not have dependencies included.
if (!pkgJson.dependencies) pkgJson['dependencies'] = {};
pkgJson.dependencies.snarkyjs = '0.*';
fs.writeJSONSync(path.join('ui', 'package.json'), pkgJson, { spaces: 2 });

// Use `install`, not `ci`, b/c these won't have package-lock.json yet.
sh.cd('ui');
await step(
fork icon29
star icon93
watch icon20

+ 64 other calls in file

249
250
251
252
253
254
255
256
257
258
{
    // clients
    const mds = ds.clientsMarkdownFiles()
    const mdo = "website/docs/api/clients"
    fs.emptyDirSync(mdo)
    fs.writeJSONSync(path.join(mdo, "_category_.json"), {
        label: "Clients",
        position: 1,
        collapsible: true,
    })
fork icon4
star icon24
watch icon9

How does fs-extra.writeJSONSync work?

fs-extra.writeJSONSync() is a synchronous method in the fs-extra module of Node.js that writes a JavaScript object or array to a JSON file on disk, overwriting the file if it already exists, and creating it if it doesn't.

289
290
291
292
293
294
295
296
297
298
});

log.verbose('debug: newTarget', newTarget);
log.verbose('debug: misses', misses);

fse.writeJSONSync(targetPkgPath, Object.assign(targetPkg, newTarget), { spaces: 2 });

if (conflicts.length > 0) {
  console.log();
  console.log('项目依赖与页面代码片段依赖发生冲突,请确认后手动合并:');
fork icon0
star icon2
watch icon2

+ 4 other calls in file

116
117
118
119
120
121
122
123
124
125
    result[brand][company].push(type)
  }
})

fs.ensureFileSync(dataFile)
fs.writeJSONSync(dataFile, result, { spaces: 2 })

ctx.body = {
  success: true
}
fork icon0
star icon0
watch icon1

+ 4 other calls in file

Ai Example

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

const data = {
  name: "John",
  age: 30,
  hobbies: ["reading", "traveling", "coding"],
};

fs.writeJSONSync("./data.json", data);

In this example, an object data is defined, which contains some data. Then fs-extra.writeJSONSync is called with two arguments: the path to the file ('./data.json') and the data object. This writes the data to a file in JSON format.

796
797
798
799
800
801
802
803
804
805
  }
  return fs.readJSONSync(file)
},

writeJSON: (file, value) => {
  return fs.writeJSONSync(file, value, {spaces: 2})
},

getGitDir: (repoDir) => {
  const dotGitPath = path.join(repoDir, '.git')
fork icon0
star icon0
watch icon1

+ 5 other calls in file

126
127
128
129
130
131
132
133
134
135
  }
}

// 写入数据
const writeJSON = (file, obj) => {
  if (!Fse.existsSync(file)) Fse.writeJSONSync(file, {});
  if (!_.isEqual(Fse.readJSONSync(file), obj)) {
    Fse.writeJSONSync(file, obj, { spaces: 2 });
    require('./modules/updateTimestamp');
    return true;
fork icon0
star icon0
watch icon1

+ 9 other calls in file

65
66
67
68
69
70
71
72
73
74
75
      );
      return;
    }


    fse.ensureDirSync(path.join(outputDir, browser.name));
    fse.writeJSONSync(path.join(outputDir, browser.name, `${Date.now()}.json`), browserRenders, {
      spaces: 2,
    });
  };
}
fork icon0
star icon0
watch icon1

+ 4 other calls in file

105
106
107
108
109
110
111
112
113
114
for (let i = 0; i < queue.length; ++i) {
  copyMissingLangObject(queue[i].source, queue[i].target, lang, queue);
}

if (queue.length > 0) {
  fs.writeJSONSync(`./public/locales/${lang}/translation.json`, target, {
    encoding: "utf-8",
    spaces: 2,
  });
}
fork icon0
star icon0
watch icon1

+ 2 other calls in file

function icon

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