我正在使用 TypeScript 开发 node.js 应用程序。
我在项目的根文件夹中创建了一个 TypeScript 文件。我运行 tsconfig
,它似乎更新了 dist
文件夹。但是,当我运行该应用程序时,我收到一条错误消息,指示未定义函数。
这里是事情变得困惑的地方:我的 src
文件夹中似乎有旧版本的 .js
和 .map
文件与我的源文件同名的相同目录。这个 .js
文件似乎有一个旧版本的文件,缺少必要的功能(类方法),与我的 /dist
文件夹中的当前版本不同。
在一天结束时,我试图对我的 /dist
文件夹中的文件运行调试器,并在我的 /src
TypeScript 文件中设置断点。
这是我在 /src
文件夹中看到的文件结构示例(此 js 文件不是当前文件):
下面是我的 /dist
文件夹的文件结构示例,其中包含已转换的 js 代码:
另外,这里是项目的网络应用程序(其余)部分的调试器设置:
最后,这是 tsconfig.json
文件的示例:
{
"compilerOptions": {
"module": "commonjs",
"target": "ES5",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": ["reflect-metadata"],
"lib": ["ES6"],
"sourceMap": true,
"inlineSources": true,
"pretty": true,
"outDir": "./dist",
"rootDir": "./src/",
"noLib": false
},
"compileOnSave": true,
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"node_modules/@types"
]
}
我想了解是哪里出了问题,导致它读取了错误的 js 文件,而不是
/dist
文件夹中的文件?我该怎么做才能修复此问题以指向
/dist
文件夹。我以为我设置的调试器设置会做到这一点,但事实并非如此。
更新:我删除了在 src 文件夹中生成的 .js 文件,它们最终返回到该文件夹,并且在进行其他更改后它们不是最新的。我不确定是什么生成了这些文件;是 webstorm 中的设置还是 tsconig.json 中的设置?
其他地方看起来不对劲。当我打开 dist 文件夹中的其中一个文件时,我发现以下代码而不是 JS 代码:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=group.js.map
这不是我所期待的,因为我期待看到转译后的 js 代码。
最佳答案
你在你的 include 和 rootDir
中声明了 src
,一切都应该相对于 rootDir
声明,但在这种情况下你可能不需要 include 或 excludes,因为你已经在 src
中包含了所有内容。 compileOnSave
选项是在您删除文件时生成文件的原因,因为已经设置了一个 watcher
来执行此操作。您还混用了 target
和 lib
javascript 版本。
如果您已经在使用 types
属性,则无需显式排除 @types
。
If types is specified, only packages listed will be included.
这是您可以尝试的清理配置
{
"compilerOptions": {
"module": "commonjs",
"target": "ES5",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": ["reflect-metadata"],
"lib": ["ES5"],
"sourceMap": true,
"inlineSources": true,
"pretty": true,
"outDir": "dist",
"rootDir": "src",
"noLib": false
},
"compileOnSave": false
}
关于node.js - WebStorm tsconfig.json 似乎没有更新正确的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089122/