相较于Visual Studio Code,你只需要允许源映射,VSCode就可以调试TypeScript,但我在WebStorm上无法做到同样的效果。
在WebStorm中,我可以轻松调试服务器端JavaScript,但无法调试TypeScript。
相较于Visual Studio Code,你只需要允许源映射,VSCode就可以调试TypeScript,但我在WebStorm上无法做到同样的效果。
在WebStorm中,我可以轻松调试服务器端JavaScript,但无法调试TypeScript。
对于任何其他在WebStorm/IDEA中调试TypeScript的人,我和OP(可能是出于不同的原因)遇到了类似的挫败感。我的问题很简单,就是我没有将工作目录设置为node运行配置中的dist
文件夹。我正在Jest中运行测试,并假设工作目录应该是我的项目根目录。将其设置为dist
后,调试开始正常工作!
更多信息...
源.ts文件在src
中
TypeScript版本:2.0.3
文件tsconfig.json
:
{
"compilerOptions": {
"jsx": "react",
"module": "commonjs",
"noImplicitAny": false,
"outDir": "dist",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es6",
"moduleResolution": "node"
},
"exclude": [
"node_modules",
"dist"
]
}
Jest配置(在package.json
中):
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/ts-jest/dist/preprocessor.js",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
}
运行配置...
工作目录:<project_root>/dist
Javascript文件:../node_modules/jest-cli/bin/jest.js
应用程序参数:--runInBand
希望这有所帮助!
outDir
对我起了作用。 - Joe Hawkins我正在尝试找到一种让Webstorm/Intellij在调试模式下监听TS文件变化并重新启动服务器的方法。看起来ts-node-dev
比nodemon
更快,因为它在重新启动之间共享了Typescript编译过程。
npm i ts-node-dev --save-dev
然后在您的运行/调试配置
中,添加一个具有以下参数的node.js
配置:
JavaScript 文件 ---> node_modules/ts-node-dev/lib/bin.js
应用程序参数 ---> --respawn -- src/script/local.server.ts
现在保存配置并使用Debug
运行,您应该能够设置断点,并在任何TS代码更改时实时重新加载服务器。
如果您恰好是使用aws lambda
进行开发,我还包装了一个小型库。
我想补充一下我的做法,使用Webstorm 2021.1.1
我发现最简单的方法是进入你的package.json,右键点击你想运行的npm脚本旁边的绿色三角形,然后选择debug。
我能够在我的typescript代码中应用断点,并且它完美地工作。由于我来自.Net,那里调试总是非常直接明了,所以我很高兴看到Webstorm也变得同样简单。
这是我选择要调试的npm脚本。
"dev": "env-cmd -f ./config/dev.env concurrently -k -n COMPILER,NODEMON -c gray,blue \"tsc -w\" \"nodemon -w dist dist/index.js\"",
tsconfig.json
中启用了源映射://tsconfig.json
{
"compilerOptions": {
...,
"sourceMap": true,
...
}
}
为了让您能够直接在TypeScript文件中设置断点,需要使用源代码映射。
然后,您应该记住使用Node选项--inspect
或--inspect-brk
。我建议在大多数情况下使用inspect-brk
,因为它会在Node启动并运行后暂停执行。
您可以在package.json
中添加简单的脚本来运行您的TypeScript Node应用程序,例如使用ts-node。
//package.json
{
"scripts": {
"dev-debug": "ts-node --inspect-brk src/index.ts"
}
}
pnpm|yarn|npm
包管理器运行该命令:#shell
pnpm run dev-debug
ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e
,你可以用它来连接到你的调试器。ts-node
的特定版本的Node。package.json
文件中首先添加:
"devDependencies": {
"ts-node": "8.1.0",
"typescript": "3.2.4"
},
npm install
,node_module/.bin/
目录将包括Windows所需的ts-node
或ts-node.cmd
版本。显然这些版本会移动。您可以查看ts-node
项目的package.json
中使用的typescript版本,以尽可能接近。然后,您可以添加断点。我唯一看到的缺点是您必须将Javascript文件(即ts文件)定义到配置中,而不仅仅是右键单击+运行。如果出现xyz is not a function
错误,请检查您的tsconfig.json
文件是否没有"noEmit": false
。为了在WebStorm(2017.2.3)中运行TypeScript源代码调试器,我进行了以下操作:
Node.js
configuration:
root/of/the/project
(where located my package.json
)dist/index.js
I am compiling my TypeScript with gulp-typescript
, but more important the source-map files. So for compiling was used task like below:
const gulp = require('gulp');
const ts = require('gulp-typescript');
const sourcemaps = require('gulp-sourcemaps');
const merge = require('merge2');
const tsProject = ts.createProject('tsconfig.json', {
declaration: true,
typescript: require('typescript'),
});
gulp.task('default', () => {
const result = gulp.src('./app/**/*.ts')
.pipe(sourcemaps.init())
.pipe(sourcemaps.identityMap()) // optional
.pipe(tsProject());
return merge([
result.js
.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '../app' }))
.pipe(gulp.dest('dist')),
result.dts
.pipe(gulp.dest('dist')),
]);
});
所有源TS文件位于"./app"文件夹中,所有编译后的文件位于./dist
文件夹中。最重要的源文件选项是sourceRoot
,填写错误的值不会指向TS文件。
通过sourcemaps.write('.', { includeContent: false, sourceRoot: '../app' }
我将我的.map
文件与.js
文件放在一起,并引用app
文件夹。我不需要.map
文件中的内容,因为已经有了 (app
文件夹)。
感谢@Ekaterina帮助我使用TypeScript运行Node调试。
//package.json
{
...
"scripts": {
"start": "npm run build && node ./dist/index.js",
"build": "webpack --config ./webpack.config.js",
...
}
dist/
目录下。入口点是index.js
。tsconfig.json
文件中启用了sourceMap
选项。//tsconfig.json
{
...
"compilerOptions": {
"sourceMap": true
}
...
}
webpack.config.js
文件中,确保你有以下所示的属性://webpack.config.js
module.exports = {
mode: 'development', // This is important!, if you set 'production' it won't let you debug
devtool: 'source-map',
...
}
运行/调试配置
:...
package.json: ~/path/to/your/package.json
Command: run
Scripts: start
...