0

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

使用 Typescript 的快速版本 ,我该如何移动node_modulesoutDir或者我会以错误的方式处理事情?

长版本 我正在尝试开始使用打字稿,配置项目似乎是最难的部分。我的目标是让我的源代码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_modulesbin/server?这似乎不对。

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

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

4

1 回答 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"
    ]
}
于 2016-12-22T17:59:00.650 回答