admin管理员组文章数量:1208155
I have monorepo (yarn workpaces) with following file structure:
├── client (workspace @client)
│ ├── package.json
│ └── tsconfig.json (extended tsconfig)
├── server (workspace @server)
│ ├── getData.ts
│ ├── package.json
│ └── tsconfig.json (extended tsconfig)
├── shared
│ └── sanitizeData.ts
├── package.json (monorepo root)
└── tsconfig.json (base tsconfig)
And I want to use function from shared/sanitizeData.ts
in server/getData.ts
I tried to use paths
from Typescript, it looks pretty straightforward according to docs, but I'm doing something wrong:
error TS2307: Cannot find module '@shared/sanitizeData'.
server/tsconfig.json
:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": "../",
"outDir": "build",
"paths": {
"@shared/*": ["shared/*"]
}
}
}
server/getData.js
:
import { sanitizeData } from "@shared/sanitizeData";
Could you help me please?
I have monorepo (yarn workpaces) with following file structure:
├── client (workspace @client)
│ ├── package.json
│ └── tsconfig.json (extended tsconfig)
├── server (workspace @server)
│ ├── getData.ts
│ ├── package.json
│ └── tsconfig.json (extended tsconfig)
├── shared
│ └── sanitizeData.ts
├── package.json (monorepo root)
└── tsconfig.json (base tsconfig)
And I want to use function from shared/sanitizeData.ts
in server/getData.ts
I tried to use paths
from Typescript, it looks pretty straightforward according to docs, but I'm doing something wrong:
error TS2307: Cannot find module '@shared/sanitizeData'.
server/tsconfig.json
:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": "../",
"outDir": "build",
"paths": {
"@shared/*": ["shared/*"]
}
}
}
server/getData.js
:
import { sanitizeData } from "@shared/sanitizeData";
Could you help me please?
Share Improve this question asked Oct 25, 2019 at 8:18 J VJ V 7632 gold badges8 silver badges12 bronze badges2 Answers
Reset to default 8Paths are relative to baseUrl
, so in your case you'd have to replace ["shared/*"]
with ["../shared/*"]
Define the baseUrl only in the root tsconfig file, to prevent rebasing complexities. Then, references all paths from baseurl.
Remember: The paths property does not merge entries from multiple tsconfig files... as the "extends" argument implies. Paths of a base tsconfig get overwritten by the paths in the derived tsconfig.
So, you'll have to copy/move the paths from the base tsconfig to any derived tsconfig files.
And, be sure to decorate any path alias with a '@' so that it's distinguishable from other relative paths. I say this, because an import from "commonlib/src" is not obvious to be a path alias, and could simply be a relative path from the local src. But, "@commonlib/src" is easily recognized as a path alias, since '@' is not a legal leading folder/file character.
本文标签: javascriptMonorepo with paths from Typescript is not workingStack Overflow
版权声明:本文标题:javascript - Monorepo with paths from Typescript is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738693540a2107242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论