参考:https://www.5axxw.com/questions/content/t8ci4x
已经在tsconfig.json 中配置了paths别名,编辑的时候引用正常,但是运行起来就报错,如下图:

方案一 tsconfig-paths
安装tsconfig-paths
npm i tsconfig-paths --save-dev
在tsconfig.json中增加属性
{
"ts-node": {
"require": ["tsconfig-paths/register"]
}
}
方案二 module-alias
安装module-alias
npm install module-alias --save
在package.json 中定义
{
"_moduleAliases": {
"@": "./src"
}
}
接着在项目入口因为这个包
require('module-alias/register');
同时满足dist运行和tsc编译
package.json
{
"_moduleAliases": {
"@": "./dist"
}
}
tsconfig.json
{
"paths":{
"@/*": ["./src/*"]
}
}
编译命令: 增加 -r tsconfig-paths/register
nodemon --exec ts-node -r tsconfig-paths/register src/index.ts
