英文:
Uncaught Error: Cannot find module './App' at webpackMissingModule
问题
我在本地环境运行npm start
命令时,在客户端目录的控制台中收到错误消息Uncaught Error: Module not found: Can't resolve ./App' and ./store in client/src.
。屏幕显示2个错误:
ERROR in ./src/index.tsx 7:0-24 Module not found: Error: Can't resolve './App.tsx' in '/client/src'
ERROR in ./src/index.tsx 7:0-24 Module not found: Error: Can't resolve './store.ts' in '/client/src'
然而,当我将项目的客户端目录导入到CodeSandbox时,它可以正常工作。
解决尝试:
- 删除
node_modules
和package-lock.json
,然后运行npm install
- 运行
npm cache clean --force
和npm cache clear --force
- 确保本地环境中的依赖和包与CodeSandbox中相同。
- 确保App组件和store模块实际存在于src/目录中。
- 检查App和store的文件名拼写是否正确,文件扩展名是否正确(例如,React组件使用.tsx)。
- 确保
文件路径
是正确的 - 在本地和CodeSandbox上都使用Chrome
发生了什么情况?
(请注意,我已经删除了代码部分,只提供翻译的内容。)
英文:
I'm receiving the error Uncaught Error: Module not found: Can't resolve ./App' and ./store in client/src.
in the console of the local environment when I run npm start
from the client directory.
The screen renders 2 errors.
ERROR in ./src/index.tsx 7:0-24 Module not found: Error: Can't resolve './App.tsx' in '/client/src'
ERROR in ./src/index.tsx 7:0-24 Module not found: Error: Can't resolve './store.ts' in '/client/src'
However, when I import the client directory of my project into a CodeSandbox, it is working.
client/src/index
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import persistStore from 'redux-persist/es/persistStore';
import setupStore from './store';
const store = setupStore();
const persistor = persistStore(store);
ReactDOM.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>,
document.getElementById('root'),
);
Solution Attempts:
- delete
node_modules
andpackage-lock.json
, thennpm install
npm cache clean --force
andnpm cache clear --force
- assured the dependencies and packages installed in my local environment are the same in CodeSandbox.
- Made sure that the App component and the store module actually exist in the src/ directory.
- Checked that the file name for both App and store is spelled correctly and that the file extension is correct as well (e.g., .tsx for React components).
- assured
file paths
are correct - using Chrome on both local and CodeSandbox
What's going on?
答案1
得分: 1
看起来你开始使用由Create React App生成的JavaScript
项目,然后尝试将项目迁移到TypeScript
。
当进行此迁移时,Create React App脚本应该会在你将任何*.js
文件重命名为*.ts
时自动生成一个tsconfig.json
文件。然而,目前似乎存在一个问题,tsconfig.json
文件没有自动生成 - 这是你看到的错误的根本原因。
解决方法很简单:手动生成tsconfig.json
文件。你可以在实际工作时由Create React App生成的示例文件参考这里:https://github.com/facebook/create-react-app/issues/10951#issuecomment-839711690。
英文:
It looks like you started with a JavaScript
project generated by Create React App and then you tried to migrate the project to TypeScript
.
When you make this migration, the Create React App scripts are supposed to automatically generate a tsconfig.json
file as soon as you rename any of your *.js
files to *.ts
. However, it seems that currently there is an issue and the tsconfig.json
file is NOT automatically generated - this is the root cause for the error you see.
The workaround is simple: generate the tsconfig.json
file manually. You can see an example file generated by Create React App when it actually works here: https://github.com/facebook/create-react-app/issues/10951#issuecomment-839711690.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论