How to run a TypeScript scratch file with imports of a project library using ts-node in WebStorm/IntelliJ?

Viewed 1783

I have a TypeScript project in IntelliJ and like to run a TypeScript scratch file to play with some code and libraries. I can run a scratch file without imports using a Node.js run configuration using --require ts-node/register as Node Parameters field (see help). However, if I try to import some project libraries, e.g.

import {of} from "rxjs/observable/of";
of('hi').subscribe(console.log);

I get TSError: ⨯ Unable to compile TypeScript because Cannot find module 'rxjs/observable/of'. I get no error if I use a regular TypeScript file in my project root (instead of a scratch file) or if I use require (instead of import), i.e.

const {Observable, of} = require("rxjs/observable/of");
of('hi').subscribe(console.log);

but I'd like to use a scratch file and the import syntax. What am I missing in my configuration?

Edit: I can not only compile the version with require without errors but also execute it without errors.

According to the help

To pass any additional parameters to ts-node (for example, --project tsconfig.json), add them in the Application parameters field.

but if I use a non existing path instead of tsconfig.json, I get no complaints that the file does not exist. Is the option even taken into account by ts-node?

1 Answers

Scratch files are stored in a special location, together with the IDE preferences and not in the project root. That's why the modules installed in the project can't be resolved when transpiling them with ts-node. We do not plan to change where the scratch files are stored as they have to be available to all projects by design.

Related