LoginSignup
10
9

More than 5 years have passed since last update.

コマンドとREPLで結果が異なるコード

Posted at

こんなコードを書いた。

index.js
console.log(!!require);
console.log(!!global.require);

なんかコマンドから実行すると、想定している動作をしないな?と思い調べてみると。

Terminal
$ node index.js
true
false
$ node
> .load index.js
console.log(!!require);
true
undefined
console.log(!!global.require);
true
undefined

どういうこっちゃ……

inspect.js
var fs = require('fs'),
    util = require('util');

fs.writeFileSync(!!global.require ? './repl.txt' : './command.txt', util.inspect(global, {
  depth: null
}));
Terminal
$ node inspect.js
$ node
> .load inspect.js
...
> .exit
$ diff -u repl.txt command.txt

ヽ(`Д´)ノなんか微妙に違うじゃん!

10
9
4

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
10
9