英文:
Create React App error - Module not found: Error: Can't resolve
问题
我有一个通过create-react-app创建的React前端应用程序。所有源文件都包括在./src
文件夹中。此外,在./functions
文件夹中,我有一个后端Firebase云函数项目,源代码位于./functions/src
中。
./functions/src/types.ts
中有一个文件,定义了前端和后端之间共享的类型。
当文件的唯一内容是类型定义时,一切都正常工作。最近,我开始添加常量值,在我的前端代码中,我收到了一个错误消息:Module not found: Error: Can't resolve '@gluetypes/types'
- 导入语句如下:import { val } from "@gluetypes/types"
我的./tsconfig.json
如下所示:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": ".",
"paths": {
"@gluetypes/*": ["./functions/src/*"],
}
},
"include": [
"src",
"functions/src",
]
}
此外,我尝试了在不使用@gluetypes别名的情况下导入它,通过import { val } from "../../functions/src/types"
,并收到了更详细的回答:
Module not found: Error: You attempted to import ../../functions/src/types which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.
英文:
I have a React front-end app created via create-react-app. All the source files are included within ./src
folder. Additionally in the ./functions
folder I have a backend Firebase cloud functions project with sources in ./functions/src
.
There is one file in ./functions/src/types.ts
that defines shared types between the front and the back end.
Everything worked fine when the only content of the file consisted of type definitions. Recently I've started adding const values and in my front-end code I've received an error Module not found: Error: Can't resolve '@gluetypes/types'
- the import statement: import { val } from "@gluetypes/types"
My ./tsconfig.json
is as follows:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": ".",
"paths": {
"@gluetypes/*": ["./functions/src/*"],
}
},
"include": [
"src",
"functions/src",
],
}
Additionally I have tried to import it without using an @gluetypes alias via import { val } from "../../functions/src/types"
and have received more descriptive answer:
Module not found: Error: You attempted to import ../../functions/src/types which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.
答案1
得分: 0
为什么会遇到这个错误
这个错误是由create-react-app的开发人员添加的特殊限制,没有直接绕过的方式。关于这个主题,你可以在这里找到更多信息。
注意
一般来说,你不应该在前端和后端之间拥有“共享类型”并将其包含在其中一个项目中,然后让另一个项目需要它,而是应该将你的类型发布到NPM,并将它们作为开发依赖项添加到你的项目中。
英文:
Why you encountered this error
The error is a special restriction added by developers of create-react-app and there is no direct way around it. You can find more about this topic here.
Note
Generally speaking you should not have "shared types" between front and backend contained in one of the projects and have the other project require it, rather you should publish your types on NPM and add them as a dev dependency to your project.
答案2
得分: 0
尝试在 tsconfig.json 文件中进行此更改。在 @ 和 gluetypes 之间添加斜杠(/)。
从
"@gluetypes/*": ["./functions/src/*"]
到
"@/gluetypes/*": ["./functions/src/*"]
英文:
Try making this change in the file tsconfig.json. Add slash(/) between @ and gluetypes.
From
"@gluetypes/*": ["./functions/src/*"]
To
"@/gluetypes/*": ["./functions/src/*"]
答案3
得分: 0
将其反过来设置,其中“桥接”类型位于前端Web应用的src/文件夹内,并在/functions服务器端代码中导入它们,问题就得以解决。更仔细调查这个问题后,我明白了可以有一种解决方案,其中这些“桥接”类型可以在src/ web应用代码内部,但这将涉及更多的工作,需要更改react-app的webpack配置。
英文:
Setting it the other way around, where the "bridging" types are within the front-end web app src/ folder and importing them in /functions server side code did the trick. Upon investigating the issue more closely, it became obvious to me that it's possible to have a solution where those "bridging" types would be inside src/ web app code, but it would entail much more work of changing react-app webpack configuration.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论