0
//test.js
console.log(this);

By executing the test.js with command 'node test.js', I got result of '{}'. But by executing the same code in REPL, I got tons of system information. Why this difference? Is it because of my configuration of Node?

user1726366
  • 2,256
  • 4
  • 15
  • 17

1 Answers1

2

Different execution contexts.

In node, any file you require is a module that has its own scope, by default an empty object (hence the {}), until you export something.

The repl, on the other hand, is its own execution context with a bunch of stuff already attached to it. You can even set things up to attach to it yourself (e.g. Convenient functions)

Paul
  • 35,689
  • 11
  • 93
  • 122