gpt4 book ai didi

javascript - Node 脚本中的可用模块

转载 作者:行者123 更新时间:2023-12-01 04:01:10 25 4
gpt4 key购买 nike

为什么当我尝试通过以下方式查找有关 Node.js 中 http 模块的更多信息时会得到不同的结果?

  1. 如果我进入 Node REPL,然后打印http模块的内容,即如果我运行

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

    我获取了 http 对象的所有详细信息:

    { IncomingMessage: 
    { [Function: IncomingMessage]
    super_:
    {
    ...
  2. 如果我编写一个名为 script.js 的脚本文件,其中包含以下一行

    console.log(http);

    并通过运行来执行它

    node script.js

    我明白了

    ReferenceError: http is not defined

我希望这两种情况的行为方式相同 - 要么预加载 http 模块,要么不预加载。为什么会有差异?我在这里犯了什么错误?

我认为我可以通过运行(在版本 2 中)预加载模块 http 来“修复”这个问题

node -r http script.js

这个模块不应该预加载 http 从而避免引用错误吗?

期待您的意见!

最佳答案

Repl 默认拥有所需的所有标准 Node.js 核心模块。

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

关于javascript - Node 脚本中的可用模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42178738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com