javascript - Typescript 项目设置(第一次)

标签 javascript transpiler visual-studio-code typescript2.1

首先,经过几个小时的谷歌搜索后,我没有看到明确的答案,如果我忽略了某些内容,我很抱歉。

快速版本 使用 Typescript 如何将 node_modules 移动到 outDir 或者我是否以错误的方式处理事情?

长版 我正在尝试开始使用 typescript ,配置项目似乎是最难的部分。我的目标是将源代码放在 src/server 中,并将输出放在 bin/server

这是我的 tsconfig.json 供引用:

{
    "compilerOptions": {
        "allowJs": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../../bin",
        "sourceMap": true,
        "suppressImplicitAnyIndexErrors": true,
        "target": "ES2015",
        "typeRoots": [
        "../../node_modules/@types/"
        ]
    },
    "exclude": [
        "bin/*",
        "node_modules/*",
        "public/*",
        "**/*-aot.ts"
    ]
}

这是目录结构:

Project
+-/bin
|  +/server
|     +-server.js
+-/src
  +/server
    +-server.ts
    +-package.json
    +-/node_modules
       +-[...]
    +-/typings
       +-[...]

要从~/Project编译,我使用tsc -p src/server,我们有bin/server/server.js

为了运行,我使用 vs code,这里是 launch.json:

{
    "version": "0.2.0",
    "configurations": [{
        "outFiles": [ "${workspaceRoot}/bin/server/**/*.js" ],
        "cwd": "${workspaceRoot}/bin/server",
        "name": "Launch",
        "type": "node",
        "request": "launch",
        "program": "${workspaceRoot}/src/server/server.ts",
        "sourceMaps": true,
        "env": {
            "NODE_ENV": "development",
            "SERVER": "http://localhost:8080"
        }
    }]
}

我收到的错误是错误:找不到模块“express”,该模块安装在src/server/node_modules/express中,所以我猜我还必须将 node_modules 移动到 bin/server 吗?这似乎不对。

typescript 的 super 新手(今天开始)感谢您花时间阅读我的长篇文章。

PS:假设一切都是最新版本。

最佳答案

找到答案!

我将 tsconfig.json 移至 src/server/ 并从项目根运行 tsc -p src/server

更新了 tsconfig.json 以供引用:

{
    "compilerOptions": {
        "allowJs": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../../bin",
        "sourceMap": true,
        "suppressImplicitAnyIndexErrors": true,
        "target": "ES2015",
        "typeRoots": ["node_modules/@types/"]
    },
    "exclude": [
        "bin/*",
        "node_modules/*",
        "public/*",
        "**/*-aot.ts"
    ]
}

关于javascript - Typescript 项目设置(第一次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41252459/

相关文章:

javascript - ui-scroll - 无法回到列表的开头

javascript - Chrome canvas 2d context measureText 给我奇怪的结果

javascript - 如何将一个 li 向右对齐并从中下拉列表?

typescript - 如何键入检查内存中的 TypeScript 代码片段?

python - 在 BigQuery 中运行 python 函数

javascript - 检查值是否在范围内并设置值是否超出范围的优雅方法?

python - 如何将 python 比较 ast 节点转换为 c?

linux - 是否有适用于 Windows 和 Linux 的 vscode 片段路径变量?

php - Xdebug V3 不会停止 VSCode 中的断点

visual-studio-code - vscode 文件图标