我可以在Node中以编程方式更改.js文件的module.exports中的JSON对象吗
Can I change a JSON object that is in module.exports of a .js file programmatically in Node?
我有一个.js
文件,其中只导出了JSON对象。我可以在更新对象时读取这些内容并将它们写回同一个文件吗?怎样我知道如何读取.json
文件,更新它并将其写回。
我在我的一个项目上做了类似的事情,但我将把这些值存储在JSON文件中,而不是使用JS文件。在对对象进行修改后,你可以做一些类似于的事情
fs.writeFile('file.json', JSON.stringify(myObj, null, 4), function(err) {
if (err) {
//handle error
}
else {
//handle success
}
});
相关文章:
- 为什么要对define.md和module.exports进行条件检查,以及条件何时通过
- 在jasmine规范中使用module.exports
- 我们可以在一个js文件中有多个module.exports语句吗
- module.exports一个jQuery插件
- “module.exports.instance”是什么意思
- Module.exports通过Mongoose调用返回undefined
- 我可以在Node中以编程方式更改.js文件的module.exports中的JSON对象吗
- 使用module.exports和ES6导出导入
- 调用module.exports上的函数时发生CommonJS非法调用错误
- 如何从module.exports访问函数
- 为什么在定义导出属性之前调用module.exports时仍然有效
- “var self = module.exports;”是什么意思
- this.prototype 和 module.exports 之間的差異
- 我们如何在nodeJS中使用module.exports返回函数
- 在定义foo之前放置“module.exports = foo”是如何工作的
- module.exports scope and node-mysql
- require():使用 module.exports vs 直接分配给 “this”
- Node.js/MongoDB:如何使用module.exports将本地主机数据库URL传递给服务器.js文件
- NodeJS prototyping with module.exports
- Expressjs JavaScript Fundamentals: exports = module.exports