0

why do I get different results when trying to find out more about the http module in node.js in the following to ways?

  1. If I enter the node REPL and then print the content of the http module, i.e. if I run

    me@mymachine:~> node
    > console.log(http)
    

    I get all the details of the http object:

    { IncomingMessage: 
        { [Function: IncomingMessage]
            super_:
              {
    ...
    
  2. If I write a script file called, say, script.js containing the following single line

    console.log(http);
    

    and execute it by running

    node script.js
    

    I get

    ReferenceError: http is not defined
    

I would have expected both cases to behave in the same way - either the http module is preloaded or not. Why is there a difference? What am I getting wrong here?

I thought I could 'fix' this by preloading module http by running (in version 2)

node -r http script.js

Shouldn't this preload module http and thus avoid the reference error?

Looking forward to your input!

1
  • The Node REPL is not just plain Node; it's a specific set of tools, and what you're observing is one of its features. Commented Feb 11, 2017 at 17:16

1 Answer 1

2

Repl has all the standard Node.js core modules required by default.

https://nodejs.org/api/repl.html#repl_accessing_core_node_js_modules

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - that explains it. Still, I am wondering why I can't preload the module using the -r parameter?
That's indeed interesting. I am not sure but I am suspecting that the -r parameter doesn't evaluate the global.fs = require('fs') as the REPL does. Someone else could shed some light here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.