LoginSignup
62
41

More than 5 years have passed since last update.

Node.jsでjsonの読み込みはrequireでできる

Posted at

Node.jsでjsonを簡単に読み込むにはrequireで読み込める。

test.json
{
  "a": "hoge",
  "b": "fuga",
  "c": "piyo"
}
test.js
var test = require( "./test.json" );

console.log( test );
$ node test.js
{ a: 'hoge', b: 'fuga', c: 'piyo' }

今まで簡単なスクリプトでもfs.readFile~~とかを書いていたけれど、
知らなかったのでメモ。

※ただしキャッシュするようなので普段使うときはfs.readFile~~のほうが良さそう。

62
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
41