Skip to content

feat(repl): support --import flag#33104

Open
seroperson wants to merge 3 commits intodenoland:mainfrom
seroperson:i23071-repl-import-flag
Open

feat(repl): support --import flag#33104
seroperson wants to merge 3 commits intodenoland:mainfrom
seroperson:i23071-repl-import-flag

Conversation

@seroperson
Copy link
Copy Markdown
Contributor

Closes #23071

  • Added --import flag (alias for --preload) for the repl command, which loads each specified module using V8 APIs and assigns its' named exports to the global scope.

I've implemented it using V8 APIs thought the same is likely possible using evaluating plain JS strings. Maybe the second approach could be simpler, just from my point of view it's bit of dirty.

It's kind of similar to --eval-file, but different in details:

  • --eval-file fetches the raw text content of a file and evaluates it as a script in the REPL context:
  • Works like pasting the file's source code into the REPL
  • All variables (including let/const locals) leak into the REPL scope
  • Relative imports inside the file break (they resolve from current directory, not the file's location)
  • Can't handle jsr: or npm: schemes

While --import:

  • Loads the file through V8's module system:
  • Proper ES module loading with full resolution, so relative imports work correctly
  • jsr:, npm:, https: specifiers work
  • Only exported names are exposed to the REPL, module-internal variables stay private

So, --import just complements the --eval-file.

How to use

# Plain import
# modules with relative imports also work correctly
# if utils.ts imports from ./other_utils.ts, it resolves from utils.ts location
deno repl --allow-read --import=./src/utils.ts
> myUtilityFunction(123)
# Import an npm package
deno repl --allow-all --import=npm:chalk
> new Chalk().red("hello")
# Import from JSR
deno repl --allow-all --import=jsr:@std/encoding
> encodeBase64("hello world")
# Combine --import with --eval for a one-liner
deno repl --allow-read --import=./src/utils.ts --eval='console.log(myUtilityFunction(123))'
# Combine --import with --eval-file for setup scripts
# setup.js can reference exports from mylib.ts
deno repl --allow-read --import=./src/mylib.ts --eval-file=./scripts/repl-setup.js

Testing

  • Added 14 integration tests covering most of what I could imagine, including: jsr resolving, npm resolving, exposing, collisions, side-effects and modules with dependencies

co-authored with Claude Opus 4.6, mostly for consulting about the codebase.

@seroperson seroperson force-pushed the i23071-repl-import-flag branch 2 times, most recently from ce3f356 to 937d5dd Compare April 6, 2026 06:22
@seroperson seroperson force-pushed the i23071-repl-import-flag branch from 937d5dd to bb7ee8e Compare April 6, 2026 06:49
@seroperson
Copy link
Copy Markdown
Contributor Author

@bartlomieju @littledivy Hello! Sorry for pinging you, but would you mind reviewing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add deno repl --import=... to import modules at startup and expose their exports

1 participant