Docs / Reference
Load Mode
A Node Snippet runs as a Module (ESM) or as a Script (CommonJS). The console offers the other one when an error calls for it.
On this page
Load Mode is how Node loads a Snippet: as an ES module, like a .mjs file, or as CommonJS, the way node file.js runs. It decides whether you write import or require, and whether the code is in strict mode.
Module or Script
| What | Module (ESM) | Script (CommonJS) |
|---|---|---|
| Loads other code with | import, export | require() |
Top-level await | Yes | No |
| Strict mode | Always | Only with "use strict" |
Module is the default. A new Node Snippet takes its Load Mode from Settings → Running → New Snippets run as. Changing the setting leaves the Snippets you already have as they are. New Snippet (⌘T or Ctrl+T) takes the Load Mode of the Snippet in front.
Strict mode changes what some code does. With n = 1 and no declaration, a Module throws ReferenceError: n is not defined, and a Script makes n a global.
When to switch
JSpark never changes the Load Mode by itself. When code needs the other mode, the Run fails and says so:
- In a Module, CommonJS such as
require,moduleorexportsthrows… is not defined. The code needs a Script. - In a Script,
importandexportare underlined in the editor, and the Run stops with import and export need a Module. The code needs a Module. - In a Script, top-level
awaitstops the Run with Top-level await needs Module. Switch to Module, or wrap it in an async function.
The switch button
Under each of these errors, the console shows a button: Switch to Script (CommonJS) or Switch to Module (ESM). Press it, and the Snippet changes mode and runs again at once.
The console is the only place to change an existing Snippet's Load Mode. The button is not shown in the Timeline.
The pictures show JSpark on a Mac. On Windows, the window looks a little different and uses Ctrl for ⌘, but everything works the same way.