It is possible in Node.js v14, but you need to use the import operator, rather than the import declaration.
$ node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let myModule;
undefined
> import("./my-module.js").then(module => { myModule = module });
Promise { <pending> }
> myModule.foo();
"bar"
Answer from diachedelic on Stack OverflowNode.js
nodejs.org › api › repl.html
REPL | Node.js v25.2.1 Documentation
Input and output may be from stdin and stdout, respectively, or may be connected to any Node.js stream. Instances of repl.REPLServer support automatic completion of inputs, completion preview, simplistic Emacs-style line editing, multi-line inputs, ZSH-like reverse-i-search, ZSH-like substring-based history search, ANSI-styled output, saving and restoring current REPL session state, error recovery, and customizable evaluation functions.
Top answer 1 of 5
78
It is possible in Node.js v14, but you need to use the import operator, rather than the import declaration.
$ node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let myModule;
undefined
> import("./my-module.js").then(module => { myModule = module });
Promise { <pending> }
> myModule.foo();
"bar"
2 of 5
64
This is not currently possible. ES modules are supposed to be imported from the ES module scope, while REPL isn't considered one. This can improve with time because the support of ES modules is experimental. The use of require and import is mutually exclusive in the Node.js module implementation, and REPL already uses require.
Dynamic import is supported in the REPL since Node.js 13. With node --experimental-repl-await, it is:
await import('./right.mjs');
GitHub
github.com › TypeStrong › ts-node
GitHub - TypeStrong/ts-node: TypeScript execution and REPL for node.js
Starred by 13.1K users
Forked by 543 users
Languages TypeScript 73.5% | JavaScript 24.9%
Node.js
nodejs.org › api › esm.html
Modules: ECMAScript modules | Node.js v25.2.1 Documentation
The resolution algorithm does not determine whether the resolved URL protocol can be loaded, or whether the file extensions are permitted, instead these validations are applied by Node.js during the load phase (for example, if it was asked to load a URL that has a protocol that is not file:, data: or node:. The algorithm also tries to determine the format of the file based on the extension (see ESM_FILE_FORMAT algorithm below).
GitHub
github.com › nodejs › node › issues › 42547
repl: -r doesn't work with ESM · Issue #42547 · nodejs/node
January 3, 2022 - I suppose that when using -r in an ESM repo, the repl must use await import instead of require for -r arguments?
Published Mar 31, 2022
GitHub
github.com › nodejs › node › commit › b1cec2cef9
doc: add esm examples to node:repl · nodejs/node@b1cec2c
January 5, 2025 - doc: add esm examples to node:replPR-URL: #55432 Reviewed-By: Tierney Cyren <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> 1 parent d6a84cf commit b1cec2c · Copy full SHA for b1cec2c · +150 · -16lines changed · Filter options · doc/api · repl.md ·
Author nodejs
GitHub
github.com › nodejs › node › pull › 55432
doc: add esm examples to `node:repl` by mfdebian · Pull Request #55432 · nodejs/node
aly Aminev) [#​55915](nodejs/node#55915) - \[[`b0ebd23e52`](nodejs/node@b0ebd23e52)] - **http2**: support ALPNCallback option (ZYSzys) [#​56187](nodejs/node#56187) - \[[`f10239fde7`](nodejs/node@f10239fde7)] - **lib**: remove redundant global regexps (Gürgün Dayıoğlu) [#​56182](nodejs/node#56182) - \[[`fd55d3cbdd`](nodejs/node@fd55d3cbdd)] - **lib**: clean up persisted signals when they are settled (Edigleysson Silva (Edy)) [#​56001](nodejs/node#56001) - \[[`889094fdbc`](nodejs/node@889094fdbc)] - **lib**: handle Float16Array in node:v8 serdes (Bartek Iwańczuk) [#&
Author nodejs
GitHub
github.com › TypeStrong › ts-node › issues › 1924
REPL with ES modules · Issue #1924 · TypeStrong/ts-node
October 31, 2022 - const u = await import('./Util') should work in the REPL when package type is "module" and Typescript is transpiling to ES modules. $ npx ts-node-esm > 2+2 /Users/ckoncz/work/git/csaba/ts-node-esm-examples/<repl>.ts:3 export {}; ^^^^^^ Uncaught SyntaxError: Unexpected token 'export' > 2+2 4 > const u = await import('./Util') Uncaught: TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
Published Dec 09, 2022
GitHub
github.com › TypeStrong › ts-node › discussions › 2029
How can one `import` an ESM module from REPL? · TypeStrong/ts-node · Discussion #2029
docker run -d --rm -it -e ... ... set -a && source ./.env && ts-node src/foo.ts # 2) ... but this, inside ts-node REPL, does NOT: > foo = await import('./src/foo.ts') How can an ESM module imported?...
Author TypeStrong
npm
npmjs.com › package › @opentf › react-node-repl
@opentf/react-node-repl - npm
import { NodeREPL } from "@opentf/react-node-repl"; import "@opentf/react-node-repl/lib/style.css"; export default function App() { const code = `console.log("Hello World")`; const deps = ["pkg1", "[email protected]", "pkg3@beta"]; return <NodeREPL code={code} deps={deps} layout="SPLIT_PANEL" />; } ... By default, in REPL mode, you cannot use import statements. You need to fallback to require(). You can run ESM modules manually in the terminal with the ESM switch on.
» npm install @opentf/react-node-repl
TypeStrong
typestrong.org › ts-node › api
ts-node
Create an implementation of node's ESM loader hooks. This may be useful if you want to wrap or compose the loader hooks to add additional functionality or combine with another loader. Node changed the hooks API, so there are two possible APIs. This function detects your node version and returns the appropriate API. Eval · Aware · Partial · Host: Pick<CreateOptions, "readFile" | "fileExists"> Defined in src/repl.ts:462 ·
Reddit
reddit.com › r/node › i want to test some sequelize models on node repl, but i am getting this error, what is the workaround?
r/node on Reddit: I want to test some sequelize models on Node REPL, but I am getting this error, what is the workaround?
May 14, 2020 - Node doesn’t support import out of the box, you’d either need to create the javascript via an mjs extension and have module turned on, or run REPL on the transpiled javascript code from either webpack or Babel, or whatever you’re using for transpiling the code. ... I'd suggest not to use a REPL. Do what you intend to do in a separete .js file and run it with esm:
GitHub
github.com › nodejs › node › issues › 44094
REPL: make -r work in ESM mode · Issue #44094 · nodejs/node
August 2, 2022 - This wouldn't break anything, since right now -r simply doesn't work in ESM mode. There is no real alternative; Right now we're using a script that uses terminal commands to stuff the terminal input buffer with await import('foo') and then runs the REPL, but that only works on Linux. ... feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.
Published Aug 02, 2022
GitHub
github.com › nodejs › node › blob › v22.3.0 › lib › repl.js
node/lib/repl.js at v22.3.0 · nodejs/node
const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) => (local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`), ... const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); return cascadedLoader.import(specifier, parentURL, importAttributes); ... 'REPL, alternatively use dynamic import: ' + toDynamicImport(ArrayPrototypeAt(self.lines, -1));
Author nodejs
Node.js
nodejs.org › en › learn › command-line › how-to-use-the-nodejs-repl
Node.js — How to use the Node.js REPL
The REPL is waiting for us to enter some JavaScript code, to be more precise. ... The first value, test, is the output we told the console to print, then we get undefined which is the return value of running console.log(). Node read this line of code, evaluated it, printed the result, and then went back to waiting for more lines of code.
GitHub
github.com › nodejs › node › issues › 19570
dynamic import can't be used in the REPL · Issue #19570 · nodejs/node
October 27, 2017 - $ ./node --experimental-modules > (node:14483) ExperimentalWarning: The ESM module loader is experimental. > import('fs').then(console.log, console.log) Promise { <pending>, domain: Domain { domain: null, _events: { removeListener: [Function: updateExceptionCapture], newListener: [Function: updateExceptionCapture], error: [Function: debugDomainError] }, _eventsCount: 3, _maxListeners: undefined, members: [] } } > { TypeError [ERR_INVALID_URL]: Invalid URL: repl at onParseError (internal/url.js:226:17) at parse (internal/url.js:235:3) at new URL (internal/url.js:318:5) at normalizeReferrerURL (internal/process/esm_loader.js:18:10) at setImportModuleDynamicallyCallback (internal/process/esm_loader.js:51:37) at process._tickCallback (internal/process/next_tick.js:114:7) input: 'repl' } 👍React with 👍12jacob-adams, patrickleet, paxperscientiam, bgmort, lin7sh and 7 more ·
Published Mar 24, 2018
GitHub
github.com › standard-things › esm
GitHub - standard-things/esm: Tomorrow's ECMAScript modules today!
node -r esm main.js · 💡 Omit the filename to enable esm in the REPL. 👏 By default, 💯 percent CJS interoperability is enabled so you can get stuff done. 🔒 .mjs files are limited to basic functionality without support for esm options. Out of the box esm just works, no configuration necessary, and supports: Passing all applicable test262 compliance tests ·
Starred by 5.3K users
Forked by 148 users
Languages JavaScript
GitHub
github.com › nodejs › node › issues › 30842
repl / eval: CommonJS globals leak into ESM modules · Issue #30842 · nodejs/node
August 17, 2019 - Now run import('./script.mjs') in repl, this produces output script.mjs function. Same for node --eval "import('./script.mjs')". Using --print in place of --eval does not change typeof require. CC @nodejs/modules-active-members · devsnek · cliIssues and PRs related to the Node.js command line interface.Issues and PRs related to the Node.js command line interface.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.experimentalIssues and PRs related to experimental features.Issues and PRs related to experimental features.replIssues and PRs related to the REPL subsystem.Issues and PRs related to the REPL subsystem.
Published Dec 07, 2019
jsDocs.io
jsdocs.io › package › ts-node
[email protected] - jsDocs.io
Filesystem host functions which are aware of the "virtual" [eval].ts, <repl>, or [stdin].ts file used to compile REPL inputs. Must be passed to create() to create a ts-node compiler service which can compile REPL inputs. type ExperimentalSpecifierResolution = 'node' | 'explicit'; type ModuleTypeOverride = 'cjs' | 'esm' | 'package';