There is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require it to use the .load command within the REPL, like such:

.load foo.js

It loads the file in line by line just as if you had typed it in the REPL. Unlike require this pollutes the REPL history with the commands you loaded. However, it has the advantage of being repeatable because it is not cached like require.

Which is better for you will depend on your use case.


Edit: It has limited applicability because it does not work in strict mode, but three years later I have learned that if your script does not have 'use strict', you can use eval to load your script without polluting the REPL history:

var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())
Answer from vossad01 on Stack Overflow
🌐
Node.js
nodejs.org › api › repl.html
REPL | Node.js v25.2.1 Documentation
The node:repl module provides a Read-Eval-Print-Loop (REPL) implementation that is available both as a standalone program or includible in other applications. It can be accessed using: import repl from 'node:repl';const repl = require('node:repl');copy
🌐
GitHub
github.com › nodejs › node › issues › 48084
REPL: import statement inside the Node.js REPL · Issue #48084 · nodejs/node
May 20, 2023 - Uncaught: SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import Instead of: import assert from 'node: assert' use: const assert = await import('node: assert')
Published   May 20, 2023
🌐
npm
npmjs.com › package › @opentf › react-node-repl
@opentf/react-node-repl - npm
The Node.js REPL in a React component. ... Your site must be served over HTTPS. The following headers must be set in your deployed page. Cross-Origin-Embedder-Policy: require-corp Cross-Origin-Opener-Policy: same-origin Learn more. ... 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" />; }
      » npm install @opentf/react-node-repl
    
Published   Apr 07, 2024
Version   0.14.0
Author   Thanga Ganapathy
🌐
Stevenloria
stevenloria.com › local-repl
Introducing local-repl: Supercharged Node.js REPLs | stevenloria.com
You specify the modules and objects that you want to automatically import in either package.json or .replrc.js.
🌐
GitHub
github.com › sloria › local-repl
GitHub - sloria/local-repl: 🐚 Project-specific configuration for the Node.js REPL
Project-specific REPLs for Node.js. local-repl allows you to automatically import modules and values into your REPL sessions with simple configuration in your project's package.json and/or .replrc.js.
Author   sloria
🌐
Node.js
nodejs.org › en › learn › command-line › how-to-use-the-nodejs-repl
Node.js — How to use the Node.js REPL
Note the difference in the outputs of the above two lines. The Node REPL printed undefined after executing console.log(), while on the other hand, it just printed the result of 5 === '5'. You need to keep in mind that the former is just a statement in JavaScript, and the latter is an expression.
🌐
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 POSTGRES_PASSWORD=postgres -e POSTGRES_DB=db -ePOSTGRES_USER=postgres -p 5433:5432 --name pg postgres:14.6-alpine3.17 npm install # 1) This works ... set -a && source ./.env && ts-node src/foo.ts # 2) ... but this, inside ts-node REPL, does NOT: > foo = await import('./...
Author   TypeStrong
Find elsewhere
🌐
npm
npmjs.com › package › local-repl
local-repl - npm
Local modules can be imported, too. ... Instead of defining configuration in "package.json", you may define your configuration in a .replrc.js file.
      » npm install local-repl
    
Published   May 06, 2018
Version   4.0.0
Author   Steven Loria
🌐
GitHub
github.com › nodejs › node › issues › 47278
REPL import statement support · Issue #47278 · nodejs/node
December 19, 2022 - What is the problem this feature will solve? Deno REPL support statement support. But Node.js REPL does not support import statement. What is the feature you are proposing to solve the problem? Nod...
Published   Mar 28, 2023
Top answer
1 of 2
14

I'm afraid that the answer is no, or at least not yet. We know that it will be supported in the future, but the ES2015 import and export won't act as in-place substitutions to CommonJS require and module.exports. This is simply because they don't work the same way. You may find more information in this Node.js blog post and this SO question. In particular, one can already run and import standard modules in Node.js, under the flag --experimental-modules (relevant 2ality blog post). However, this feature will still not work from the REPL.

Your idea of using babel-node (which is now called babel-cli) was not that bad: babel implements an interoperable require function, which is kind of a shim that can retrieve modules designed for either type system. Unfortunately, the official website claims lack of support for this functionality in the REPL:

ES6-style module-loading may not function as expected

Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.

Nonetheless, this issue does not apply to production code, which should not rely on the REPL anyway. Instead of using the REPL, you may consider writing a script in a bare-bones project containing babel-cli, and create a script for running the transpiled version:

{
  "name": "my-test-chamber",
  "private": true,
  "scripts": {
    "build": "babel src/main.js -o lib/main.js",
    "start": "npm run build && node lib/main.js"
  },
  "dev-dependencies": {
    "babel-cli": "^6.0.0"
  }
}
2 of 2
0

I found a way to import the ES6 Modules into REPL (I don't remember where):

PS> node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
> const {platform, type} = await import("node:os");
undefined
> console.log('Platform:',platform(), 'Type:', type());
Platform: win32 Type: Windows_NT
undefined
>
> const path = await import("node:path");
undefined
> path.sep
'\\'
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-to-use-node-js-repl
How to use Node.js REPL ? - GeeksforGeeks
July 23, 2025 - There are a couple of modules in the Node.Js REPL by default. You can get the list by pressing the TAB key twice. If you want to import other modules, you need to follow the following procedure: You need to firstly install the package via the npm package manager for Node.Js.
🌐
GitHub
github.com › nodejs › node › blob › main › doc › api › repl.md
node/doc/api/repl.md at main · nodejs/node
import repl from 'node:repl'; const msg = 'message'; repl.start('> ').context.m = msg;
Author   nodejs
🌐
GitHub
github.com › nodejs › node › issues › 19570
dynamic import can't be used in the REPL · Issue #19570 · nodejs/node
October 27, 2017 - esmIssues and PRs related to the ... the REPL subsystem.Issues and PRs related to the REPL subsystem. ... $ ./node --experimental-modules > (node:14483) ExperimentalWarning: The ESM module loader is experimental. > import('fs').then(console.log, console.log) Promise { <pending>, ...
Published   Mar 24, 2018
🌐
Deno
docs.deno.com › api › node › repl
repl - Node documentation
Node · repl · import * as mod ... not supported. The node:repl module provides a Read-Eval-Print-Loop (REPL) implementation that is available both as a standalone program or includible in other applications....
🌐
Bun
bun.com › modules › node:repl › replserver › definecommand
REPLServer.defineCommand method | Node.js repl module | Bun
3 weeks ago - import repl from 'node:repl'; const replServer = repl.start({ prompt: '> ' }); replServer.defineCommand('sayhello', { help: 'Say hello', action(name) { this.clearBufferedCommand(); console.log(`Hello, ${name}!`); this.displayPrompt(); }, }); ...
Top answer
1 of 2
27

You cannot use static import statements (e.g. import someModule from "some-module"), at the moment. I'm not aware of any efforts/tickets/pull requests/intents to change that.

You can use import() syntax to load modules! This returns a promise. So for example you can create a variable someModule, start importing, & after done importing, set someModule to that module:

let someModule
import("some-module")
  .then( loaded=> someModule= loaded)

Or you can directly use the import in your promise handler:

import("some-module").then(someModule => someModule.default())

For more complex examples, you might want to use an async Immediately Invoked Function Expression, so you can use await syntax:

(async function(){
    // since we are in an async function we can use 'await' here:
    let someModule = await import("some-module")
    console.log(someModule.default())
})()

last, if you start Node.JS with the --experimental-repl-await flag, you can use async directly from the repl & drop the async immediately invoked function:

let someModule = await import("some-module")

// because you have already 'await'ed
// you may immediately use someModule,
// whereas previously was not "loaded"
console.log(someModule.default())
2 of 2
0

Given the static nature of import statements and that the engine needs to know all static imports (before processing any non-import related code). You can see how the import statement really isn't compatible with interactive REPL support.

For example, import statements are allowed in the middle or end of a file, but they're hoisted and processed before "non-static-import related code".

What should the REPL do if you type a static import statement at the end of your REPL session?

It can't easily go back and rerun all your prior commands in light of this potentially fundamental change!

I didn't think about that until reading this. Thanks Kevin Qian who also has ideas.

🌐
O'Reilly
oreilly.com › library › view › learning-node-2nd › 9781491943113 › ch04.html
Interactive Node with REPL and More on the Console - Learning Node, 2nd Edition [Book]
While you’re exploring the use of Node and figuring out the code for your custom module or Node application, you don’t have to type JavaScript into a file and run it with Node to test your code. Node also comes with an interactive component known as REPL, or read-eval-print loop.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-node-js-repl
How To Use the Node.js REPL | DigitalOcean
March 18, 2022 - The REPL is bundled with every Node.js installation and allows you to quickly test and explore JavaScript code within the Node environment without having to store it in a file.