How to use the readJSON function from fs-extra

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

fs-extra.readJSON is a function that reads and parses a JSON file from the file system and returns a JavaScript object.

13
14
15
16
17
18
19
20
21
22
await execa("cp", ["./neutralino.js", "./dist/neutralino.js"]);
// run neu build command
await execa("pnpm", ["exec", "neu", "build"]);

// build done read neutralino.config.js file
const config = await fs.readJSON(
  path.resolve(process.cwd(), "neutralino.config.json")
);
const isOverseaVersion = process.env["YAAGL_OVERSEA"] == "1";
const bundleId = isOverseaVersion
fork icon15
star icon187
watch icon0

155
156
157
158
159
160
161
162
163
164
  spaces: 2,
});
await fs.remove(sysPath.join(root, "jsconfig.json"));
await fs.remove(sysPath.join(root, "jsconfig.server.json"));

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, {
fork icon5
star icon36
watch icon0

+ 3 other calls in file

How does fs-extra.readJSON work?

fs-extra.readJSON works by using the fs.readFile method from the Node.js fs module to read a JSON file from the file system and then parsing the contents of the file using the JSON.parse method. When called with a file path as its first argument, fs-extra.readJSON will return a promise that resolves to the parsed JavaScript object contained in the JSON file. If an error occurs while reading or parsing the file, the promise will be rejected with an error. Under the hood, fs-extra.readJSON uses the fs.readFile method with an encoding of 'utf8' to read the contents of the file as a string. It then passes the resulting string to the JSON.parse method to parse it as JSON and return the resulting JavaScript object. fs-extra.readJSON is part of the fs-extra package, which is an extension of the Node.js fs module that provides additional functionality for working with the file system.

32
33
34
35
36
37
38
39
40
41
outputJsonAsync = (fname, obj) => fse.outputFile(getAbsPath(fname),JSON.stringify(obj))
outputJsVarAsync = (fname, varName, obj) =>
                      fse.outputFile(getAbsPath(fname),"export var " +varName + " = " +JSON.stringify(obj))
outputFileAsync = (fname, data) => fse.outputFile(getAbsPath(fname),data)
readFileAsync = (fname, encoding) => fse.readFile(getAbsPath(fname),encoding)
readJsonAsync = fname => fse.readJSON(getAbsPath(fname))
readdirAsync = dirname => fse.readdir(getAbsPath(dirname))
statAsync = path => fse.stat(getAbsPath(path))
readJson = fname => fse.readJSON(getAbsPath(fname))
pathExistsAsync = fname => fse.pathExists(getAbsPath(fname))
fork icon1
star icon3
watch icon3

+ 3 other calls in file

8
9
10
11
12
13
14
15
16
17
18
19


async function sendTelemetry(action) {
  let json = {};


  try {
    json = await fs.readJSON(dest);
  } catch {
    // Ignore
  }

fork icon0
star icon1
watch icon1

+ 2 other calls in file

Ai Example

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

async function readMyJsonFile() {
  try {
    const myJson = await fs.readJSON("path/to/my/json/file.json");
    console.log(myJson);
  } catch (err) {
    console.error(err);
  }
}

readMyJsonFile();

In this example, we use fs-extra.readJSON to read a JSON file located at 'path/to/my/json/file.json'. We use the await keyword to wait for the promise returned by fs-extra.readJSON to resolve with the parsed JSON object. If an error occurs while reading or parsing the file, we catch the error and log it to the console. When the function is executed, it will log the contents of the JSON file to the console as a parsed JavaScript object. Note that fs-extra.readJSON automatically parses the JSON file into a JavaScript object, so we can access the properties of the object directly.

226
227
228
229
230
231
232
233
234
235

if (options.exec && fs.existsSync(`app/modules/lib/${options.exec}.json`)) {
    let parentData = this.data;
    this.data = {};
    this.scope = this.scope.create({ $_PARAM: this.parse(options.params) });
    await this.exec(await fs.readJSON(`app/modules/lib/${options.exec}.json`), true);
    data = this.data;
    this.scope = this.scope.parent;
    this.data = parentData;
} else {
fork icon0
star icon1
watch icon1

+ 4 other calls in file

499
500
501
502
503
504
505
506
507
508
    this.meta = clone(cfg.meta);
    await validator.init(this, this.meta);
}

if (fs.existsSync('app/modules/global.json')) {
    await this.exec(await fs.readJSON('app/modules/global.json'), true);
}

/*
debug('body: %o', this.req.body);
fork icon0
star icon1
watch icon1

+ 3 other calls in file

355
356
357
358
359
360
361
362
363
364
);

const excludeList = [];
const prePath = path.resolve(changesetPath, 'pre.json');
if (await fs.pathExists(prePath)) {
  const data = await fs.readJSON(prePath);
  // Only exclude changesets in pre-release mode.
  if (data.mode === 'pre') {
    excludeList.push(...data.changesets.map(name => `${name}.md`));
  }
fork icon0
star icon0
watch icon1

110
111
112
113
114
115
116
117
118
119
120
  });
  done();
}


async function addExtensionDependencies(done) {
  const packageJson = await readJSON("package.json");
  packageJson.extensionDependencies = ["ms-vscode.cpptools"];
  await writeJSON("package.json", packageJson, { spaces: 2 });
  done();
}
fork icon0
star icon0
watch icon1

+ 13 other calls in file

6
7
8
9
10
11
12
13
14
15
16
17
18
const lodash = require('lodash');


const ARTIFACT_SERVER = 'https://s3.eu-central-1.amazonaws.com/mui-org-ci';


async function loadCurrentSnapshot() {
  return fse.readJSON(path.join(__dirname, '../../size-snapshot.json'));
}


/**
 * @param {string} commitId - the sha of a commit
fork icon0
star icon0
watch icon1

+ 8 other calls in file

43
44
45
46
47
48
49
50
51
52
const repoRoot = path.join(__dirname, '..');
const recipesFolder = path.join(repoRoot, 'recipes');
const outputFolder = path.join(repoRoot, 'archives');
const allJson = path.join(repoRoot, 'all.json');
const featuredFile = path.join(repoRoot, 'featured.json');
const featuredRecipes = await fs.readJSON(featuredFile);
let recipeList = [];
let unsuccessful = 0;

await fs.ensureDir(outputFolder);
fork icon0
star icon0
watch icon1

71
72
73
74
75
76
77
78
79
80
 * @returns {Promise<Object>}
 */
async function readComponentsProperties(componentsPropertiesFilePath) {
   if (!componentsProperties) {
      if (await fs.pathExists(componentsPropertiesFilePath)) {
         componentsProperties = await fs.readJSON(componentsPropertiesFilePath);
      } else {
         componentsProperties = {};
      }
   }
fork icon0
star icon0
watch icon0

+ 7 other calls in file

58
59
60
61
62
63
64
65
66
67
const tag = version.includes('next') ? 'next' : 'main';
const tagPath = path.resolve('versions', 'v1', 'tags', tag);

// Check if there's an existing version for the tag, and that it's not newer than the one we're adding
if (await fs.pathExists(tagPath)) {
  const currentTag = await fs.readJSON(
    path.resolve(tagPath, 'manifest.json'),
  );
  if (semver.gt(currentTag.releaseVersion, version)) {
    console.log(
fork icon0
star icon0
watch icon0

function icon

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