我已经使用 Typescript 和 JSX 创建了一个 React 应用程序(创建 .tsx 文件而不是 .ts),我尝试在具有热重载的 Docker 容器中运行它,但收效甚微。
我尝试过将 nodemon 与 ts-node 结合使用,但仍然遇到错误
[ERR_UNKNOWN_FILE_EXTENSION]:未知的文件扩展名“.tsx”
一种建议是从 package.json 文件中删除“type”:“module”,但这会导致错误;
语法错误:无法在模块外部使用 import 语句
有谁知道如何解决让 ts-node 处理 tsx 文件的具体问题,或者更一般地说如何解决在 docker 容器内使用 JSX 热重载 typescript 的问题?
我有这个简单的代码来尝试工作线程。
// thread.ts
import { Worker, isMainThread } from 'worker_threads';
if (isMainThread) {
const worker = new Worker(__filename);
} else {
console.log('hello from worker');
}
Run Code Online (Sandbox Code Playgroud)
显然,当我尝试使用 运行该文件时ts-node thread.ts,出现此错误:
(node:13200) 警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名。
语法错误:无法在模块外部使用 import 语句
但是如果我手动编译并调用该文件tsc && dist/thread.js,它就可以正常工作
我尝试进一步实验,如果我将要在工作线程内运行的代码放在外部文件中,那么它也可以正常工作
// thread.ts
import { Worker, isMainThread } from 'worker_threads';
const worker = new Worker('./src/worker.ts');
Run Code Online (Sandbox Code Playgroud)
这是我的 tsconfig.json
{
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript …Run Code Online (Sandbox Code Playgroud) 假设我们有两个具有以下结构的node.js项目:

Foo 项目通过在其 中声明以下内容将 Bar 作为依赖项package.json:
{
"scripts": {
"start": "ts-node src/index.ts --transpile-only --no-lazy"
},
"dependencies": {
"bar": "file:../Bar"
}
}
Run Code Online (Sandbox Code Playgroud)
我使用以下命令配置 VSCode 的调试器launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "start",
"request": "launch",
"runtimeArgs": [
"start"
],
"runtimeExecutable": "npm",
"smartStep": true,
"type": "node"
}
]
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我在调试期间进入从 Bar 导入的函数时,无论我将 Bar 的包入口定义为或,它都会进入临时编译的 js 源而不是原始的 TypeScript 源。此外,如果我在 Bar 的打字稿源中添加断点,它们甚至不会被加载。如何让调试器加载 Bar 的断点,并在调试 Foo 时直接进入 TypeScript 源?我应该修改 中的一些属性,还是在启动脚本中将更多选项传递给 ts-node ?
为了更好地说明这个问题,我将两个演示项目上传到了 GitHub:StackOverflow-Demomain …
node.js typescript visual-studio-code ts-node vscode-debugger
我正在使用TypeScript为Express应用程序供电.
我有nodemon实例化节点与--inspect选项.
这是我的tsconfig.json
{
"compilerOptions": {
"target": "es6",
"lib": ["es2017"],
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noResolve": false,
"noEmitOnError": false,
"noImplicitAny": true,
"declaration": true,
"sourceMap": true,
"outDir": "dist/",
"moduleResolution": "node",
"skipLibCheck": true
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
的package.json
"ts-node": "^5.0.1",
"typescript": "^2.7.2"
Run Code Online (Sandbox Code Playgroud)
为什么WebStorm不会遇到正确的断点并检查req/res?
有没有办法使用TS-节点与WebWorkers但没有使用的WebPack?
当我做:
const worker = new Worker('path-to/workerFile.ts', { // ... });
我得到:
TypeError [ERR_WORKER_UNSUPPORTED_EXTENSION]:
The worker script extension must be ".js" or ".mjs". Received ".ts" at new Worker (internal/worker.js:272:15)
// ....
有任何想法吗?
托默
全新的 Mean Stack,但已经通过遵循各种教程在 dockerized 环境中成功设置了一个。我主要使用了https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf 中解释的那个
我遇到的问题是,自动编译对 TypeScript 文件的更改并重新启动服务失败,我不确定配置中缺少什么。我已经尝试使用 nodemon 和 ts-node-dev 进行各种迭代,但没有成功。
服务器启动并 100% 提供页面服务,但代码更改不会触发任何内容。
下面是 Dockerfile(用于 Node Express 服务器)、package.json 和我的 tsconfig.json,如果有人能指出我哪里出错了,那将是一个很大的帮助。我还展示了整个堆栈的 docker-compose.yml 文件。
文件
FROM node:latest
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
RUN npm install --save body-parser express mongoose
RUN npm install --save nocache
RUN npm install …Run Code Online (Sandbox Code Playgroud) 我在开发时遇到了 ts-node 的问题。
我想测试一些东西,所以如你所知,评论是我最好的朋友。但是使用 ts-node 我遇到了这个错误:
'foo' is declared but its value is never read
Run Code Online (Sandbox Code Playgroud)
但我不想评论所有未使用的变量,因为这些变量实际上对测试后的代码很有用。
那么,是否有类似ts-node --please-let-me-work忽略这些错误的解决方案?
谢谢 ;)
当我运行时,ts-node node_modules/jasmine/bin/jasmine 我收到这些错误:
tsc/globals.ts:7:12 - error TS2304: Cannot find name 'SugarcubeState'.
7 State: SugarcubeState;
~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
这是该全局文件:
/* eslint-disable @typescript-eslint/no-explicit-any */
console.log("global.ts");
// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace NodeJS {
interface Global {
State: SugarcubeState;
setup: {};
}
}
declare const State: SugarcubeState = {
variables: {}
};
declare const setup: any = {
variables: {}
};
Run Code Online (Sandbox Code Playgroud)
这是我的 index.d.ts:
type SugarcubeVariables = {
};
type SugarcubeState = { variables: SugarcubeVariables };
Run Code Online (Sandbox Code Playgroud)
它们都在同一个目录中,Visual Studio 代码没有抱怨任何东西。为什么 ts-node 似乎找不到我的类型定义文件?
我用谷歌搜索并找到了这个网站:https : //github.com/TypeStrong/ts-node#help-my-types-are-missing按照它的建议,我修改了我的 …
我有一个 TypeScript 项目,其中的测试是用 TypeScript 编写的。我想用来mocha直接测试TS文件。我为此使用了 ts-node,如ts-node#mocha 中所述。在项目中,我使用了一个没有 TS 类型定义的 JS 库。所以我d.ts为此创建了一个文件。编译和运行项目时一切正常。但是摩卡咖啡失败了:
% yarn mocha --require ts-node/register --require source-map-support/register ./test/*.ts
src/client.ts:3:26 - error TS7016: Could not find a declaration file for module 'algosdk'. '/home/robert/projects/algorand/ts-mocha/node_modules/algosdk/index.js' implicitly has an 'any' type.
Try `npm install @types/algosdk` if it exists or add a new declaration (.d.ts) file containing `declare module 'algosdk';`
3 import * as algosdk from "algosdk";
Run Code Online (Sandbox Code Playgroud)
看来 ts-node 无法识别这些d.ts文件。
唯一有效的解决方案是使用require而不是import语句,但这不会将类型链接到 algosdk。 …
这是我试图运行的命令./bitgo-express --port 3080 --env test --bind localhost,我收到以下错误:
(node:367854) ExperimentalWarning: The ESM module loader is experimental.
internal/process/esm_loader.js:90
internalBinding('errors').triggerUncaughtException(
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for /home/root/example.com/BitGoJS/modules/express/bin/bitgo-express
at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
at Loader.getFormat (internal/modules/esm/loader.js:116:42)
at Loader.getModuleJob (internal/modules/esm/loader.js:247:31)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Loader.import (internal/modules/esm/loader.js:181:17)
at async Object.loadESM (internal/process/esm_loader.js:84:5) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
Run Code Online (Sandbox Code Playgroud)
bitgo-快递:
#!/usr/bin/env node
const { init } = require('../src/expressApp.ts');
if (require.main === module) {
init()
.catch(err => {
console.log(`Fatal error: ${err.message}`);
console.log(err.stack);
});
}
Run Code Online (Sandbox Code Playgroud)
expressApp.ts:
/** …Run Code Online (Sandbox Code Playgroud)