将Postgres与新创建的React应用程序一起使用时出错:找不到模块:无法解析“ dns” [英] Error using Postgres with a newly created React app: Module not found: Can't resolve 'dns'

查看:49
本文介绍了将Postgres与新创建的React应用程序一起使用时出错:找不到模块:无法解析“ dns”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能在这里遗漏了一些非常明显的东西,但是当尝试从新创建的React应用程序使用Postgres时,我不断收到以下错误消息



找不到模块:无法在'/ Users / tarek / test / node_modules / pg / lib'中解析'dns'



此处复制步骤如下:


  1. 使用create-react-app创建新的React应用

  2. 使用 npm install pg --save

  3. 添加Postgres
    模块到
    App中。 js: const {Client} = require('pg');

  4. 键入 npm run start ,您会收到上述错误。

我在这里错过了什么?



**节点版本:v8.12.0



** package.json

  {
name: test,
version: 0.1.0,
private:true,
依赖项:{
pg: ^ 7.4.3,
react: ^ 16.5.1,
react-dom: ^ 16.5。 1,
react-scripts: 1.1.5
},
scripts:{
start: react-scripts start,
build: react-scripts build,
test: react-scripts test --env = jsdom,
eject:反应脚本弹出
}
}


解决方案

问题是React应用旨在在浏览器中运行,并且浏览器无法直接访问数据库。即使您确实找到了执行此操作的方法,它也只能在本地运行,而不能在生产中使用。考虑一下,如果数据库在您的计算机上,而您的应用程序在Internet上可用,则远程用户将无法以您所描述的方式访问计算机上的数据。



要在React中使用数据库,您需要设置服务器-可能是用Node编写的(使用 Express )。它将为您的应用程序和数据提供服务。



服务器接收到来自不同用户的请求并以适当的资源进行响应。例如,如果他们访问您的网站(例如example.com),则您的服务器应针对您的项目使用index.html进行响应(在这种情况下,它应为React应用程序的index.html)。然后,要从数据库中获取数据,您可以使用 example.com/api/model/1 这样的路由来设置api,以便您的react应用可以请求<您服务器上ID为 1 的code> model ,它将从数据库中抓取并将其发送到React应用。 / p>

请查看MERN(Mongo Express React Node)堆栈,以详细了解所有这些如何协同工作。即使使用Postgres,该过程也与使用Mongo几乎相同。


I am probably missing something very obvious here, but I keep getting the following error when trying to use Postgres from a newly created React app

Module not found: Can't resolve 'dns' in '/Users/tarek/test/node_modules/pg/lib'

Here are the steps to reproduce:

  1. Create a fresh React app using create-react-app
  2. Add the Postgres module using npm install pg --save
  3. Add the following line to App.js: const { Client } = require('pg');
  4. Type npm run start and you'll get the above error.

What am I missing here?

**Node version: v8.12.0

** package.json

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "pg": "^7.4.3",
    "react": "^16.5.1",
    "react-dom": "^16.5.1",
    "react-scripts": "1.1.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

解决方案

The issue is that React apps are meant to run in the browser, and browsers can't access databases directly. Even if you did find a way to do this, it would only work locally and not in production. Consider that if the database is on your machine and your app available on the internet, there would be no way for a remote user to get access to the data on your machine in the way you are describing.

In order to use a database with React, you'll need to set up a server—possibly written in Node (with Express)—that will serve your application and data for it.

The server receives requests from different users and responds with the appropriate resources. For example if they go to your website (e.g. example.com), your server should respond with index.html for your project (in this case it would be the index.html for your React app). Then to get data from your database, you can set up an api with routes like example.com/api/model/1 so that your react app can request the instance of model with id 1 from your server, which will grab it from the database and send it over to the React app.

Look into the MERN (Mongo Express React Node) stack for more detail on how all these work together. Even with Postgres the process will be pretty much the same as with Mongo.

这篇关于将Postgres与新创建的React应用程序一起使用时出错:找不到模块:无法解析“ dns”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆