> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/concept/env/node.js.md).

# Node.js

[JS](/web/js.md) ⟩ [concepts](/web/browser/concepts.md) ⟩ [environment](/web/js/concept/env.md) ⟩ Node

{% hint style="success" %}
(a JS <mark style="color:yellow;">**runtime**</mark> [environment](/web/js/concept/env.md)) builds <mark style="color:yellow;">**server-side**</mark> applications with JavaScript.
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="danger" %}
in <mark style="color:purple;">**Node.js**</mark>：&#x20;

* [top-level scope](/web/js/scope/top-level.md) of a file is [module scope](/web/js/scope/module.md) ( <mark style="color:red;">**not**</mark> [global scope](/web/js/scope/global.md):exclamation:)
* the [global object](/web/js/scope/global/object.md) is refered to as "[global](/web/js/scope/global/object/global.md)".
  {% endhint %}

{% hint style="info" %}
in <mark style="color:purple;">**Node.js**</mark>：

* each <mark style="color:yellow;">**JS file**</mark> is treated as a [module](/web/js/module.md).&#x20;
* <mark style="color:orange;">**automatically**</mark>**&#x20;**<mark style="color:yellow;">**wraps**</mark> the **code of a&#x20;**<mark style="color:orange;">**JS file**</mark> in a self [IIFE](/web/js/val/func/expr/iife.md) with：

  * <mark style="color:blue;">**exports**</mark>, <mark style="color:blue;">**require**</mark>, <mark style="color:blue;">**module**</mark>, <mark style="color:blue;">**\_\_filename**</mark>, <mark style="color:blue;">**\_\_dirname**</mark>&#x20;

  as <mark style="color:yellow;">**parameters**</mark> to the function.

👉 stackoverflow ⟩ [What is the 'global' object in NodeJS](https://stackoverflow.com/a/43630222/5409815)
{% endhint %}

{% hint style="success" %}
[v8](/web/js/concept/env/js-engine/v8.md) is the [JS engine](/web/js/concept/env/js-engine.md) used in <mark style="color:yellow;">**Chrome**</mark> / [Node.js](/web/js/concept/env/node.js.md).
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}

* [scope](/web/js/scope.md)
  * [module scope](/web/js/scope/module.md) - [top-level scope](/web/js/scope/top-level.md) of a JS file.
  * [global](/web/js/scope/global/object/global.md) - the global object in <mark style="color:purple;">**Node.js**</mark> environment.
* [module](/web/js/module.md)
  * <mark style="color:red;">**each**</mark>**&#x20;**<mark style="color:orange;">**JS file**</mark> is treated as a [module](/web/js/module.md).&#x20;
    {% endtab %}

{% tab title="📗 參考" %}

* [ ] [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩ Ch. 4 ⟩ [Node](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch4.md#node)
  {% endtab %}

{% tab title="📘 手冊" %}

* [ ] MDN ⟩ [Node.js](https://developer.mozilla.org/en-US/docs/Glossary/Node.js)
* [ ] [Node.js](https://nodejs.org/en/) ⟩ ["global"](https://nodejs.org/api/globals.html#globals_global)&#x20;
  {% endtab %}

{% tab title="🗣 討論" %}

* [What is the 'global' object in NodeJS](https://stackoverflow.com/a/43630222/5409815)
  {% endtab %}
  {% endtabs %}
