哦,我阅读了编辑后的问题并检查了您的项目。以下是我所做的更改:
我使用了tsconfig-paths 模块:这将允许使用根导入别名正确构建应用程序并映射到文件系统中的物理路径。
更新的 tsconfig.json
{
"compilerOptions": {
"target": "es6",
"preserveConstEnums": true, //<----change
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"baseUrl": "./src", //<----change
"esModuleInterop": true,
"paths": {
"@library/*": [
"myFolder/*" //<----change
],
}
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
".vscode"
]
}
更新的 package.json
{
"name": "test2",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"start:dev": "nodemon",
"start": "node --inspect=5858 -r tsconfig-paths/register -r ts-node/register ./src/server.ts", //<----change
"build": "tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"tsconfig-paths": "^3.9.0" //<----change
},
"devDependencies": {
"@types/express": "^4.17.8",
"@types/node": "^14.11.1",
"concurrently": "^5.3.0",
"nodemon": "^2.0.4",
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
},
"nodemonConfig": {
"ignore": [
"**/*.test.ts",
"**/*.spec.ts",
".git",
"node_modules"
],
"watch": [
"src"
],
"exec": "npm start",
"ext": "ts"
}
}
还有一个名为 tsconfig-paths-bootstrap.js 的附加文件(与 tsconfig.json 相邻),内容如下:
const tsConfig = require('./tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');
const baseUrl = './build';
tsConfigPaths.register({
baseUrl,
paths: tsConfig.compilerOptions.paths,
});
一切顺利!
希望对您有所帮助!