简体   繁体   English

ts-node应该检查baseUrl tsconfig.json属性吗?

[英]Should ts-node examine the baseUrl tsconfig.json property?

I'm using ts-node and in tsconfig.json I have "baseUrl": "./src" . 我正在使用ts-node而在tsconfig.json我有"baseUrl": "./src" This way I can import a sibling in src without using ./ . 这样我就可以在不使用./情况下导入src的兄弟。 However ts-node does not seem to using the baseUrl property in resolving the sibling imports, so I get errors like this: 但是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)

Curious whether this is a ts-node bug or whether I should be doing something different? 好奇这是否是一个ts节点错误或我是否应该做一些不同的事情?

As indicated in the issue posted to ts-node tsconfig-paths can be used for this. 如发布到ts-node的问题所示, tsconfig-paths可用于此目的。 I have a fully (But minimal) working example here. 我在这里有一个完全(但很小)的工作示例。 If you want to try it out just: 如果你想尝试一下:

git clone https://github.com/oleersoy/tsmochanyc
cd tsmochanyc
npm i
npm test

Look at the the baseUrl and paths propertis in tsconfig.json to see how the resolution works. 看的baseUrlpaths propertis在tsconfig.json看到的分辨率是如何工作的。 In this case they have been set so that tsmochanyc proxies src/ this way the path mirrors the same paths that devs would use if tsmochanyc were an external dependency. 在这种情况下,它们已设置为tsmochanyc代理src/这样路径镜像devs将使用的相同路径,如果tsmochanyc是外部依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tsconfig.json 仅在某些时候尊重 baseUrl 绝对导入 - tsconfig.json honoring baseUrl absolute imports only some of the time tsconfig.json 中 TARGET 属性的影响 - Effect of TARGET property inside tsconfig.json 如何在 tsconfig.json 中排除以“.spec.ts”结尾的文件 - How to exclude files ending in '.spec.ts' in tsconfig.json lib属性会覆盖tsconfig.json中的目标属性吗 - does lib property overwrite target property in tsconfig.json TypeScript tsconfig.json文件中使用typeRoots与baseUrl的相对路径 - relative path using typeRoots vs. baseUrl in TypeScript tsconfig.json file 如何从 tsconfig.json 发布 npm package 荣誉 baseUrl - how to have published npm package honor baseUrl from tsconfig.json 如果使用Webpack,是否应该在tsconfig.json中指定根文件? - Should I specify root files in tsconfig.json if Webpack is used? tsconfig.json中的“ target”属性实际上代表什么? - What does the “target” property in tsconfig.json actually represent? tsconfig.json 中的 **/node_modules/* 是什么意思? - What it means **/node_modules/* in tsconfig.json? 为什么 Vite 会创建两个 TypeScript 配置文件:tsconfig.json 和 tsconfig.node.json? - Why does Vite create two TypeScript config files: tsconfig.json and tsconfig.node.json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM