How to run/debug scratch file (Javascript/Typescript)

I want to use scratch file as a test polygon. Is it possible to ru a .js/.ts file:

const result = 1 + 1
console.log(result)

Would be fired.

6 comments
Comment actions Permalink

You can run a JavaScript scratch file using the corresponding action in its right-click menu:

 

 

Typescript scratch files can't be run for the same reason as non-scratch .ts files: Typescript can't be executed directly, it has to be transpiled to JavaScript first

0
Comment actions Permalink

Thanks, can i use something like ts-node to run my .ts scratch files?

0
Comment actions Permalink

You can try using --require ts-node/register in your run configuration, like:

 

 

of course, you have to install ts-node in your project first

2
Comment actions Permalink

Thanks, it works! I also can have `ts-node` installed globally? To remove a need to install it in every project.

0
Comment actions Permalink

requiring global modules doesn't usually work, unless you use special tricks like NODE_PATH;

note that requiring global modules is not recommended:

You are highly encouraged to place your dependencies locally in node_modules folders. They will be loaded faster, and more reliably.

0
Comment actions Permalink

Just trying to run the file created an SQL run configuration for me. It told me "Error: No configured target data sources", which doesn't make sense.

Instead, I created a local package.json that contained this line, and then I clicked the play button next to that.

"scripts": {    "scratch_javascript": "node scratch_javascript.js"}
1

Please sign in to leave a comment.