【问题标题】:Typescript project setup (first time)打字稿项目设置(第一次)
【发布时间】:2016-12-23 01:59:00
【问题描述】:

首先,经过数小时的谷歌搜索,我没有看到明确的答案,如果我忽略了某些内容,请见谅。

快速版 使用 Typescript,我如何将 node_modules 移动到 outDir 还是我做错了事情?

加长版 我正在尝试开始使用打字稿,配置项目似乎是最难的部分。我的目标是让我的源代码在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代码,这里是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"
        }
    }]
}

我得到的错误是Error: Cannot find module 'express',模块安装在src/server/node_modules/express,所以我猜我也必须将node_modules移动到bin/server?这似乎不对。

打字稿的超级新手(从今天开始)感谢您花时间阅读我的长文。

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

【问题讨论】:

    标签: javascript visual-studio-code transpiler vscode-settings typescript2.1


    【解决方案1】:

    找到答案!

    我将 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"
        ]
    }
    

    【讨论】:

    • 点击打勾变成问题的解决方案。
    猜你喜欢
    • 2020-01-16
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2016-09-09
    相关资源
    最近更新 更多