【发布时间】:2020-05-20 20:45:41
【问题描述】:
我正在运行 VS Code,目前正在尝试在我的 typescript 项目上设置一些别名。
我的开发设置基于 nodemon 和 ts-node,代码被编译到 dist 文件夹。
到目前为止,我成功让 Typescript Hero 使用别名管理导入:
到目前为止,我的文件夹结构是:
.
└─┬ src
├──modules
├────Category
├────Ressource
├──shared
├────debug
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"target": "es6",
"outDir": "./dist",
"baseUrl": "./src",
"paths": {
"@shared/*": [
"shared/*"
],
"@modules/*": [
"modules/*"
]
},
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.test.ts",
]
}
这是第一个失败的别名导入。
//Server.ts file
import Print from '@shared/debug/Print.class';
import App from './App';
const MyApp: App = new App();
MyApp.ExpressApp.listen(MyApp.Config.ExpressPort, () => {
Print.Log('Express server listening on port ' + MyApp.Config.ExpressPort);
});
但是,我收到一个错误:“Cannot find module '@shared/debug/Print.class'” on "cross-env NODE_ENV=development nodemon ts-node ./src/server. ts”。
这就是我的立场。
现在,我已经阅读了一些关于 SO 的 Q&A,似乎即使我设法使别名在 dev 中工作,它在生产中也会失败,因为我是从 Typescript src 文件夹中运行的,我的可交付成果是内置 dist ?如果是这样,有什么办法可以补救吗?非常感谢
【问题讨论】:
-
您能否确认在共享/调试文件夹中有一个 Print.class.ts 文件
-
我确实有这样的文件,目前它只是在开发/暂存环境中进行控制台日志。
标签: node.js typescript visual-studio-code tsconfig-paths