我正在使用 ts-node,在 tsconfig.json
中我有 "baseUrl": "./src"
。这样我就可以在 src
中导入同级而不使用 ./
。但是 ts-node 似乎没有使用 baseUrl
属性来解析同级导入,所以我得到这样的错误:
> tsmochanyc@1.0.0 test /home/ole/Junk/tsmochanyc
> mocha -r ts-node/register src/**/*.spec.ts
Error: Cannot find module 'hello'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)
想知道这是 ts-node 错误还是我应该做一些不同的事情?
最佳答案
如发布到 ts-node tsconfig-paths 的问题中所示可以用于此。我在这里有一个完整的(但最小的)工作示例。如果您想尝试一下:
git clone https://github.com/oleersoy/tsmochanyc
cd tsmochanyc
npm i
npm test
查看 tsconfig.json
中的 baseUrl
和 paths
属性,了解解析的工作原理。在这种情况下,它们已被设置为 tsmochanyc
以这种方式代理 src/
路径反射(reflect)了如果 tsmochanyc
是一个开发人员将使用的相同路径外部依赖。
关于javascript - ts-node 是否应该检查 baseUrl tsconfig.json 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958749/