执行完tsc后,用node运行js发现报错了
Error: Cannot find module '@/xxx'
原因
在tsconfig中配置了paths, 但是typescript 不会对 别名进行额外的转换
"paths": {
"@/*": ["./src/*"]
}
解决
- 使用苦 module-alias
在package.json配置跟 webpack alias 的路径指向然后在入口文件调用引用方法即可
// Aliases
"_moduleAliases": {
"@root" : ".", // Application's root
"@deep" : "src/some/very/deep/directory/or/file",
"@my_module" : "lib/some-file.js",
"something" : "src/foo", // Or without @. Actually, it could be any string
}
// 在入口文件最顶端调用即可
require('module-alias/register')
- 使用webpack 或其他编译工具