Closed xccjk closed 1 year ago
const str = JSON.stringify(data, "", "\t");
或者
const str = JSON.stringify(data, null, "\t");
读取:
const getPackageJson = (path) => {
const packageJson = fs.readFileSync(path);
return JSON.parse(packageJson);
}
packageJson('./package.json');
写入:
const writePackageJson = ({ path, json }) => {
fs.writeFile(path, JSON.stringify(json, null, '\t'), (err) => {
if (err) console.log('package.json修改失败: ', err);
console.log('package.json修改成功!');
})
}
writePackageJson({ path: './package.json', json: packageJson('./package.json') })
fs.existsSync(path)
__dirname is not defined in ES module scope in JS
在
es
模块中使用__dirname
时,会报错解决方案: