首先,经过几个小时的谷歌搜索后,我没有看到明确的答案,如果我忽略了某些内容,我很抱歉。
快速版本
使用 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/