Home > Software engineering >  REPL for Typescript with application loaded
REPL for Typescript with application loaded

Time:12-19

How can I get a REPL for a project generated using create-react-app with the Typescript flag?

I've installed ts-node which runs but does not seem to include the code in the project. Is there a command using that or something else that will get me a REPL with my application preloaded? Looking for something equivalent to rails console.

CodePudding user response:

No, but its not hard to wire up yourself. The difference from the rails console is that in ruby everything is more or less global, or accessible from something global. But in JS, you import specific things from specific files, so many functions/classes could have identical names.

This all means that you must decide what code from your application is available to the console by default.


You want node:repl from the node standard library.

Make file called console.log somewhere.

import * as repl from 'node:repl';

const replServer = repl.start('> ')
replServer.context.thingYouWantToBeLocal = { abc: 123 };

Add a script to your package.json:

"console": "ts-node ./console.ts"

And then:

npm run console

Which should pop up a prompt. And if you type:

console.log(thingYouWantToBeLocal)

Then you should get back

{ abc: 123 }
  • Related