Scripts and modules — one piece of code under three sets of rules¶
Level: 201 · for anyone who has seen code work in one file and fail in another
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: The same line prints object 1 false as a script and undefined undefined true as a module: a module is always strict, its top-level this is undefined, and its var never becomes a property of globalThis.
Keywords: --input-type, script goal, module goal
What the finished page will answer¶
- What does one line print for
typeof this,globalThis.vand a plain function'sthisunder--input-type=commonjsand under--input-type=module? - Where does a
.cjsfile fit, given that the same line printsobject undefined falsethere? - Which syntax is legal only in a script (an HTML-like
<!--comment,awaitas a variable name,with) and which only in a module (import,export, top-levelawait)? - How does Node choose the rules for an
-estring with no flag, and what changes when the string containsimportor top-levelawait?
Examples it will need¶
-
scripts_and_modules_same_line_sh.sh— one line run as a script, as a .cjs file and as a module, printing typeof this, globalThis.v and a plain function's this for each -
scripts_and_modules_syntax_sh.sh— an HTML-like comment, await as a variable name and top-level await, each tried as script and as module, with the output or the SyntaxError
See also¶
- Strict mode — the strict rules every module gets without asking
- The global object — which top-level declarations land on
globalThis - ES modules and CommonJS — where CommonJS fits between the two
package.json— how Node picks the rules for a file