javascript 无法编译反应应用程序,因为它说“找不到模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47933951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:42:40  来源:igfitidea点击:

Unable to compile react application as it says "Module not found"

javascriptreactjsweb-applicationscreate-react-app

提问by Pruthvi Raj Gowda

I have been trying to execute a very simple react application, but for some reason I'm unable to compile it. When i try to compile it in bash using "npm start", i get the error stating "Failed to compile: ./src/index.js Module not found: Can't resolve './registerServiceWorker' in '/Users/Pruthvi/Desktop/ReactApplication/my-app/src'"

我一直在尝试执行一个非常简单的反应应用程序,但由于某种原因我无法编译它。当我尝试使用“npm start”在 bash 中编译它时,我收到错误消息“编译失败:./src/index.js 模块未找到:无法解析‘/Users/Pruthvi’中的‘./registerServiceWorker’ /Desktop/ReactApplication/my-app/src'"

can anybody help me as I am a new bee to react application!

任何人都可以帮助我,因为我是一个新的蜜蜂来反应应用程序!

回答by Jules Dupont

By default, create-react-appwill create a progressive web app, as documented here. The progressive web app relies on registerServiceWorker()in the registerServiceWorker.jsfile. You have apparently deleted this file, so your project no longer builds.

默认情况下,create-react-app将创建一个渐进式网络应用程序,如此处所述。渐进式Web应用程序依赖于registerServiceWorker()registerServiceWorker.js文件。您显然已删除此文件,因此您的项目不再构建。

You have two choices:

你有两个选择:

  1. Keep your project as a progressive web app, in which case you should simply restore the original state you had after first creating.
  2. Opt out of the progressive web app features.
  1. 将您的项目保持为渐进式 Web 应用程序,在这种情况下,您应该简单地恢复首次创建后的原始状态。
  2. 选择退出渐进式 Web 应用程序功能。

In order to opt out of the progressive web app features, you need to modify your src/index.jsfile as follows:

为了选择退出渐进式 Web 应用程序功能,您需要src/index.js按如下方式修改您的文件:

  1. Delete the importof the registerServiceWorker.
  2. Delete the registerServiceWorker()call.
  1. 删除importregisterServiceWorker
  2. 删除registerServiceWorker()通话。

Your src/index.jswill then look like this:

src/index.js然后你的会是这样的:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
// Deleted -- import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
// Deleted -- registerServiceWorker();

Once you've made those modifications to your index.js, you can safely delete the registerServiceWorker.jsfile (which it appears you've already done based on the error message you've posted).

对 进行了这些修改后index.js,您可以安全地删除该registerServiceWorker.js文件(根据您发布的错误消息,您似乎已经完成了该操作)。

The documentation gives a more complete description of how to opt outand some of the consequences of having a progressive web app.

该文档更完整地描述了如何选择退出以及拥有渐进式 Web 应用程序的一些后果

回答by mostafa tourad

If you are new to react and need to build apps fast you can try create-react-app by facebook

如果您不熟悉并需要快速构建应用程序,您可以尝试 通过 facebook 创建反应应用程序

it a command line tool that will help you build better react app fast with zero configuration you can learn more from the link above and its easy to use

它是一个命令行工具,可以帮助您以零配置快速构建更好的反应应用程序,您可以从上面的链接中了解更多信息,并且它易于使用

also make sure that you use a good editor that can help you catch errors and to help you and give you good answer the second time please provide your code to understand the problem well .

还要确保您使用了一个好的编辑器,它可以帮助您发现错误并帮助您并在第二次为您提供好的答案请提供您的代码以很好地理解问题。