[英]"Module not found: Can't resolve..." to custom module - TS2307
We've inherited a Next + TypeScript site from another developer and we're trying to create a new component that's based very heavily from one that already exists.我们从另一个开发人员那里继承了一个 Next + TypeScript 站点,我们正在尝试创建一个新的组件,该组件非常依赖于已经存在的组件。 I've copied the entire component and sub folders to the same level as the existing one, but if I then change the reference to the cloned folder, I get the "Module not found: Can't resolve" error:
我已将整个组件和子文件夹复制到与现有文件夹相同的级别,但如果我随后更改对克隆文件夹的引用,我会收到“找不到模块:无法解析”错误:
import { MyModule } from '@project-root/shared/foo/bar/old/module';
works, but有效,但是
import { MyModule } from '@project-root/shared/foo/bar/new/module';
in the same file, doesn't.在同一个文件中,没有。 But the contents of the 'new' folder is directly copied from the 'old' folder.
但是“新”文件夹中的内容是直接从“旧”文件夹中复制的。
I've copied the reference to the path to the tsconfig.base.json and restarted but it's not being found:我已将对路径的引用复制到 tsconfig.base.json 并重新启动但未找到:
"@project-root/shared/foo/bar/old/module": [
"libs/shared/foo/bar/old/module/src/index.ts"
],
"@project-root/shared/foo/bar/new/module'": [
"libs/shared/foo/bar/new/module/src/index.ts"
],
What's the cause of the issue, and how can it be resolved?问题的原因是什么,如何解决?
As you suggested here is my answer.正如你在这里建议的那样是我的答案。 The apostrophe at the end of "@project-root/shared/foo/bar/new/module'" is the issue.
“@project-root/shared/foo/bar/new/module'”末尾的撇号是问题所在。 Also happened to me once, somebody else who had just looked at it found it immediatly...
我也遇到过一次,刚刚别人看了一眼就发现了。。。
@DerAnonyme found the issue. @DerAnonyme发现了问题。 You have an extra apostrophe where you shouldn't (at the end of the path string for the new path mapping's key ("
module'
"))- I'd wager due to a bad copy-paste from the import statement:你有一个额外的撇号,你不应该(在新路径映射的键(“
module'
”)的路径字符串的末尾) - 我打赌由于导入语句中的错误复制粘贴:
"@project-root/shared/foo/bar/new/module'": [
"libs/shared/foo/bar/new/module/src/index.ts"
],
I could repro getting the same error message.我可以重现同样的错误信息。
tsconfig.json tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@project-root/shared/foo/bar/old/module": ["A.ts"],
"@project-root/shared/foo/bar/new/module'": ["B.ts"]
}
}
}
A.ts and B.ts A.ts 和 B.ts
export const foo = 0;
index.ts索引.ts
import { foo as fooA } from "@project-root/shared/foo/bar/old/module"
import { foo as fooB } from "@project-root/shared/foo/bar/new/module"
error (notice how there's no error for line 1 with the no-typo path mapping):错误(请注意第 1 行没有错字路径映射的错误):
index.ts:2:29 - error TS2307: Cannot find module '@project-root/shared/foo/bar/new/module' or its corresponding type declarations.
2 import { foo as fooB } from "@project-root/shared/foo/bar/new/module"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And then, removing the erroneous apostrophe character, the error goes away.然后,删除错误的撇号字符,错误就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.