当我尝试运行 ts-node-dev./index.ts 时,出现以下错误,我更新了我的打字稿和 ts-node-dev、ts-node,但如果我删除具有 @types 的依赖项,此错误会继续弹出/XXXX(例如:@types/express)它工作正常。
错误表达式:传递给 的非字符串值ts.resolveTypeReferenceDirective
,可能是由使用过时签名的包装包造成的resolveTypeReferenceDirectives
。这可能不是TS本身的问题。
"dependencies": {
"ajv": "^8.9.0",
"axios": "^0.27.1",
"exceljs": "^4.3.0",
"express": "^4.17.1",
"json2csv": "^5.0.7",
"moment-timezone": "^0.5.33",
"mysql": "^2.18.1",
"mysql-utilities": "^1.1.3",
"q": "^1.5.1",
"ts-node": "^10.8.0",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.2",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5"
},
"devDependencies": {
"@types/chai": "^4.2.22",
"@types/express": "^4.17.11",
"@types/jest": "^27.4.0",
"@types/mocha": "^9.0.0",
"@types/mysql": "^2.15.20",
"@types/node": "^15.6.1",
"chai": "^4.3.4",
"jest": "^27.4.7",
"mocha": "^9.1.2",
"ts-jest": "^27.1.3"
},
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行在常规文件夹中创建的带有两个 .ts 文件的脚本。一份包含脚本,一份包含运行脚本的辅助函数。我还导入了更多东西,例如 axios 或表单数据。
问题是,当我尝试使用 ts-node: node script.ts运行脚本时,出现以下错误:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
Run Code Online (Sandbox Code Playgroud)
我的package.json:
{
"dependencies": {
"@types/node": "^17.0.23",
"axios": "^0.26.1",
"form-data": "^4.0.0",
"showdown": "^2.0.3",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
},
"type": "module"
}
Run Code Online (Sandbox Code Playgroud)
还有我的 tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true
},
"include": ["/**/*.ts"],
"exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
我在 script.ts 文件中的导入是:
import { datoManagementPrimaryEnvironment } from "./content.management";
import {
createContent,
uploadToCloudfare,
getEntryFromDatoWithTheId,
getFilters,
} from "./helpers";
Run Code Online (Sandbox Code Playgroud)
在 helpers.ts 中:
import { datoManagementPrimaryEnvironment } from "./content.management";
import axios from "axios"; …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的模拟服务器设置别名。每当我尝试编译ts
文件时,它都会返回找不到正确模块的错误,即使这些模块是在tsconfig,json
->中定义的paths
文件夹结构:
??? server
? ??? src
? ???/json
??? src
? ???/modules
??? tsconfig.json
Run Code Online (Sandbox Code Playgroud)
这是我的 tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"experimentalDecorators": true,
"jsx": "react",
"lib": [
"dom",
"es2015",
"es2015.promise"
],
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"paths": {
"@project/app/modules/*": [
"modules/*"
],
"@project/server/data/*": [
"../server/src/json/*"
]
},
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"tools"
]
}
Run Code Online (Sandbox Code Playgroud)
错误:
Error: Cannot find module '@project/server/data/accounts/accountsList'
我在我的Vs Code终端和命令提示符中出现错误,即'ts-node'未被识别为内部或外部命令,可操作程序或批处理文件.当我在终端npm run dev中尝试启动命令时,我也添加了我的package.json文件.
{
"name": "tsnode",
"version": "1.0.0",
"description": "ts-node experiment.",
"scripts": {
"dev": "nodemon --exec 'ts-node --cache-directory .tscache' ./server.ts",
"start": "ts-node --fast ./server.ts"
},
"author": "Mugesh",
"license": "ISC",
"dependencies": {
"@types/body-parser": "^1.16.3",
"@types/chalk": "^0.4.31",
"@types/express": "^4.0.35",
"@types/node": "^7.0.18",
"body-parser": "^1.17.1",
"chalk": "^1.1.3",
"express": "^4.15.2",
"nodemon": "^1.11.0",
"ts-node": "^3.0.4",
"typescript": "^2.3.4"
}
Run Code Online (Sandbox Code Playgroud)
}
我正在寻找一种方法来使用npm脚本同时运行tsc --watch && nodemon --watch
.我可以独立运行这些命令,但是当我想要运行它们时,只执行第一个命令.例如.如果我有这个脚本:
"scripts": {
"runDeb": "set NODE_ENV=development&& tsc --watch && nodemon --watch"
}
Run Code Online (Sandbox Code Playgroud)
tsc --watch
被执行但从nodemon
未被调用,反之亦然.
在提出这个问题之前,我检查了类似的主题并尝试了典型的解决方案。
\n我知道 TypeScript 配置中的常见原因是什么"module": "ESXXXX"
。\n就我而言,我遇到了错误
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for D:\\IntelliJ IDEA\\XXXXXX\\node_modules\\tsconfig-paths\\src\\__tests__\\config-loader.test.ts\n
Run Code Online (Sandbox Code Playgroud)\n"module": "ESnext"
在和两种"module": "CommonJS"
情况下。
典型的解决方案之一是使用ts-node/esm。\n首先,此功能是实验性的。接下来,它只是将一个错误替换为另一个错误:
\n(node:24788) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time\n(Use `node --trace-warnings ...` to show where the warning was created)\n\n\xc3\x97 ERROR: CustomError: Cannot find module 'D:\\IntelliJ IDEA\\XXXXX\\node_modules\\tsconfig-paths\\register' imported from D:\\IntelliJ IDEA\\XXXXX\\node_modules\\mocha\\lib\\nodejs\\esm-utils.js\n
Run Code Online (Sandbox Code Playgroud)\nextension:\n - ts\n\nspec: …
Run Code Online (Sandbox Code Playgroud) 我偶然发现可以在 javascript 中向 Error 构造函数添加原因。
但是,当我尝试使用此功能时,我的应用程序无法启动,因为它不知道这个“新”构造函数参数。
> tsc && node dist/index.js
promo/promo-service/am-promo-request-handler.ts:43:104 - error TS2554: Expected 0-1 arguments, but got 2.
43 throw new Error(`Can't read Maxmind GeoLite2 City db from mmdb file '${config.pathMmdbCity}'`, { cause: err});
Found 1 error in promo/promo-service/am-promo-request-handler.ts:43
Run Code Online (Sandbox Code Playgroud)
以下所有命令均因上述编译错误而停止
nodemon
tsc && node dist/index.js
ts-node index.ts
Run Code Online (Sandbox Code Playgroud)
我将以下脚本添加到我的 package.json 中(确保询问正确的节点实例和其他工具的版本)
"check": "nodemon -v && node -v && tsc -v && ts-node -v && npm -v"
Run Code Online (Sandbox Code Playgroud)
它返回
2.0.19
v16.14.2
Version 4.7.4
v10.9.1
8.17.0
Run Code Online (Sandbox Code Playgroud)
该功能应该从节点版本 10.9.0 …
我是打字稿和表达的新手。我正在尝试使用运行最简单的快速应用程序ts-node-dev
,但出现以下错误。
> ./node_modules/.bin/ts-node-dev src/index.ts 16:07:40
[INFO] 16:07:42 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.7.2)
Compilation error in /home/lht/microservice/ticketing/auth/src/index.ts
Error: Debug Failure. False expression: Non-string value passed to `ts.resolveTypeReferenceDirective`, likely by a wrapping package working with an outdated `resolveTypeReferenceDirectives` signature. This is probably not a problem in TS itself.
at Object.<anonymous> (/home/lht/microservice/ticketing/auth/src/index.ts:1:7)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module._compile (/home/lht/microservice/ticketing/auth/node_modules/source-map-support/source-map-support.js:568:25)
at Module.m._compile (/tmp/ts-node-dev-hook-8101223397369532.js:69:33)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at require.extensions.(anonymous function) (/tmp/ts-node-dev-hook-8101223397369532.js:71:20)
at Object.nodeDevHook [as .ts] (/home/lht/microservice/ticketing/auth/node_modules/ts-node-dev/lib/hook.js:63:13)
at Module.load (internal/modules/cjs/loader.js:653:32) …
Run Code Online (Sandbox Code Playgroud) 我正在研究一个相对较大的打字稿项目,我正在使用它ts-node
来运行节点测试和示例.据我所知,ts-node
将ts
文件编译成js
文件并执行.
最近我听说过deno
,这是一个打字稿运行时.我尝试了一些使用的打字稿中的一些例子ts-node
.我运行了这个例子deno
,在控制台中打印了许多编译消息,然后执行代码.后来我发现里面有缓存文件/username/.deno
.我觉得deno
执行速度不快ts-node
这似乎都deno
和ts-node
编译和使用高速缓存运行.他们之间有什么区别?
我不明白为什么ts-node
在启用 esm 时无法解析别名
我做了一个小项目试图尽可能地隔离问题
package.json
{
"type": "module"
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"module": "es2020",
"baseUrl": "./",
"paths": {
"$lib/*": [
"src/lib/*"
]
},
},
"ts-node": {
"esm": true
}
}
Run Code Online (Sandbox Code Playgroud)
test.ts
import { testFn } from "$lib/module"
testFn()
Run Code Online (Sandbox Code Playgroud)
lib/module.ts
export function testFn () {
console.log("Test function")
}
Run Code Online (Sandbox Code Playgroud)
命令
import { testFn } from "$lib/module"
testFn()
Run Code Online (Sandbox Code Playgroud)
ts-node ×10
typescript ×8
javascript ×3
node.js ×3
alias ×2
nodemon ×2
tsc ×2
tsconfig ×2
deno ×1
express ×1
mocha.js ×1
npm ×1
package.json ×1
ts-node-dev ×1