[英]How to run a TypeScript scratch file with imports of a project library using ts-node in WebStorm/IntelliJ?
I have a TypeScript project in IntelliJ and like to run a TypeScript scratch file to play with some code and libraries.我在 IntelliJ 中有一个 TypeScript 项目,并且喜欢运行一个 TypeScript 临时文件来处理一些代码和库。 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 ).我可以使用 Node.js 运行配置使用--require ts-node/register
as Node Parameters 字段运行一个暂存文件,而无需导入(请参阅帮助)。 However, if I try to import some project libraries, eg但是,如果我尝试导入一些项目库,例如
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'
.我得到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
), ie如果我在项目根目录中使用常规 TypeScript 文件(而不是临时文件)或者使用require
(而不是import
),则不会出现错误,即
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.但我想使用临时文件和import
语法。 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.编辑:我不仅可以无错误地编译带有require
的版本,而且还可以无错误地执行它。
To pass any additional parameters to ts-node (for example,
--project tsconfig.json
), add them in the Application parameters field.要将任何其他参数传递给 ts-node(例如,----project tsconfig.json
),请将它们添加到应用程序参数字段中。
but if I use a non existing path instead of tsconfig.json
, I get no complaints that the file does not exist.但是如果我使用不存在的路径而不是tsconfig.json
,我不会抱怨该文件不存在。 Is the option even taken into account by ts-node
? ts-node
甚至考虑了该选项吗?
Scratch files are stored in a special location, together with the IDE preferences and not in the project root.临时文件与IDE 首选项一起存储在一个特殊位置,而不是在项目根目录中。 That's why the modules installed in the project can't be resolved when transpiling them with ts-node
.这就是为什么安装在项目中的模块在使用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.我们不打算更改临时文件的存储位置,因为它们必须按照设计对所有项目可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.