简体   繁体   English

如何在 WebStorm/IntelliJ 中使用 ts-node 导入项目库来运行 TypeScript 暂存文件?

[英]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的版本,而且还可以无错误地执行它。

According to the help根据帮助

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.

相关问题 如何使用 ts-node 在终端的 nextjs 项目中运行 typescript / tsx 文件? - How do I run a a typescript / tsx file from nextjs project in terminal using ts-node? 如何使用ts-node运行测试文件? - How do I run a test file using ts-node? ts-node调试打字稿功能如何进入ts文件 - ts-node debug typescript function how to step into ts file 如何在无需全局安装 ts-node 或 npx 的情况下完全运行 nodemon + ts-node + typescript? - How to run nodemon + ts-node + typescript altogether without having to install ts-node or npx globally? 在 TypeScript (TS-Node) 中运行 glob 命令 - Run glob command in TypeScript (TS-Node) 为什么 ts-node 不编译打字稿文件? - Why is ts-node not compiling typescript file? 在 TypeScript 中使用绝对导入时的 ts-node 'MODULE_NOT_FOUND' - ts-node 'MODULE_NOT_FOUND' when using absolute imports in TypeScript 如何在 ts-node 中使用项目依赖 - How to use project dependencies with ts-node 使用VS Code调试在Docker容器中使用ts节点运行的TypeScript应用程序时,如何正确设置断点? - How to be able to set up breakpoints correctly when using VS Code to debug a TypeScript app run using ts-node in a Docker container? 使用 ts-node 执行 typescript 文件时出错(将项目安装为全局模块后) - Error while executing typescript file with ts-node (after installing the project as global module)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM