Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9140

[Node.js]requireで、確実にあるのに中身が空になってしまう(エラーは出ない)

$
0
0

環境

node:v8.11.3

プログラム

こんな感じでリポジトリがあるとして、確かにファイルはある。なかったらエラー出ますからね。。。

hogeRepository.js
constuserRepository=require('../user/userRepository.js');console.log(userRepository)//出力結果は、{}

原因

この指定しているuserRepository.jsでもrequireを書いているプログラム「hogeRepository.js」をrequiteしてしまっていた。

つまり、お互いをrequireをしてしまうと、どちらかは空になってしまうようだ。

解決方法

そもそもお互いをrequireし合っていたのはミスだったのでそれ直したらなおった。


Viewing all articles
Browse latest Browse all 9140

Trending Articles