Uncaught Error: 无法找到模块 './App',在 webpackMissingModule

huangapple go评论76阅读模式
英文:

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_modulespackage-lock.json,然后运行npm install
  • 运行npm cache clean --forcenpm 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 and package-lock.json, then npm install
  • npm cache clean --force and npm 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.

huangapple
  • 本文由 发表于 2023年2月19日 15:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定