简体   繁体   English

找不到模块:错误:使用 MongoDB 时无法解析“dns”

[英]Module not found: Error: Can't resolve 'dns' when using MongoDB

I'm new to Reactjs, Nodejs and MongoDB. I'm currently trying to change Mediums snowflake tool to store users scores in a database.我是 Reactjs、Nodejs 和 MongoDB 的新手。我目前正在尝试更改 Mediums 雪花工具以将用户分数存储在数据库中。 I have installed yarn, mongodb and mongodb-core through npm. It is a single page web application which is what I think is causing me trouble.我已经通过 npm 安装了 yarn、mongodb 和 mongodb-core。这是一个单页 web 应用程序,我认为这给我带来了麻烦。 I add我加

var MongoClient = require('mongodb');

To SnowflakeApp.js and encounter the following error:SnowflakeApp.js ,遇到如下错误:

Module not found: Error: Can't resolve 'dns' in '/home/mlAHO174/snowflake/node_modules/mongodb-core/lib'找不到模块:错误:无法解析 '/home/mlAHO174/snowflake/node_modules/mongodb-core/lib' 中的 'dns'

I've tried googling this error and have discovered it could be a range of things.我试过用谷歌搜索这个错误,发现它可能是一系列的事情。 I'm not sure if it is because React is front end and I'm trying to alter back end or because mongoDB is installed incorrectly.我不确定是因为 React 是前端而我正在尝试更改后端还是因为 mongoDB 安装不正确。 I'm new to this so would be grateful for help!我是新来的,所以会很感激帮助!

DNS is a core module of Node.JS. DNS 是 Node.JS 的核心模块。 Telling people they need to install DNS via NPM will end up with them having a completely different module that does something else.告诉人们他们需要通过 NPM 安装 DNS 最终会让他们拥有一个完全不同的模块来做其他事情。

https://nodejs.org/api/dns.html vs https://www.npmjs.com/package/dns https://nodejs.org/api/dns.htmlhttps://www.npmjs.com/package/dns

This error most likely means you are trying to do something from the client-side that needs to be done on the server-side.此错误很可能意味着您正在尝试从客户端执行需要在服务器端完成的操作。 If MongoDB module can't find the DNS component, it's running on the client-side.如果 MongoDB 模块找不到 DNS 组件,则它在客户端运行。

MongoDB has to run on the server. MongoDB 必须在服务器上运行。 In order to access data from React dynamically you'll need to set up an API using something like Express or Apollo.为了从 React 动态访问数据,您需要使用 Express 或 Apollo 之类的东西设置 API。

I solved this by installing and using 'bson' instead of 'mongodb' for the client part of the code.我通过为代码的客户端部分安装和使用 'bson' 而不是 'mongodb' 解决了这个问题。 'bson' has a tiny bit of what 'mongodb' has and it might have what you are looking for. 'bson' 有一点点 'mongodb' 的东西,它可能有你正在寻找的东西。 'bson' is built for the browser. 'bson' 是为浏览器构建的。

In my case I needed the "ObjectId" in the browser and pulling it in from 'bson' did the trick as I didn't want to reference 'mongodb' because of the error described in the OP.在我的情况下,我需要浏览器中的“ObjectId”并将其从“bson”中拉入,因为我不想引用“mongodb”,因为 OP 中描述了错误。

The other answers are also correct depending on why you're getting this error.其他答案也是正确的,具体取决于您收到此错误的原因。

The problem is that you are trying to connect to the database from the front end.问题是您正在尝试从前端连接到数据库。 If this were possible that would open up a whole world of security issues.如果这是可能的,那将打开整个安全问题的世界。 You need to set up your database connections on the backend and then have the front end make requests to the backend to handle the database.您需要在后端设置数据库连接,然后让前端向后端发出请求以处理数据库。

For anyone who encounters this Error while importing the clientPromise (like in the with-mongodb template):对于在导入 clientPromise 时遇到此错误的任何人(如在 with-mongodb 模板中):

Make sure you're in the /pages/ directory!确保您在 /pages/ 目录中!

It won't work in other directories like /components.不会在 /components 等其他目录中工作

(and you should take a break or get some coffee...) (你应该休息一下或喝点咖啡......)

I think - mongo package is meant to be run on servers only, not in the browser.我认为 - mongo package 只能在服务器上运行,而不是在浏览器中运行。

It does not work in Next.js pages file components too, but does work in getStaticProps , getServerSideProps , getStaticPaths etc - because they run on the server, not the client.它在 Next.js 页面文件组件中也不起作用,但在getStaticPropsgetServerSidePropsgetStaticPaths等中起作用——因为它们在服务器上运行,而不是客户端。

Alternative - use Firebase Realtime database, you can access it in client-side code too.替代方案 - 使用 Firebase 实时数据库,您也可以在客户端代码中访问它。 Example - a website (say a React app) that is hosted on GitHub pages or some other static server, but doesn't have a web app server (aka backend).示例 - 托管在 GitHub 页面或其他一些static服务器上但没有web 应用程序服务器(也称为后端)的网站(比如 React 应用程序)。

welcome to stack overflow.欢迎堆栈溢出。

You need to understand and learn few basics of web-applications.您需要了解和学习 Web 应用程序的一些基础知识。 There's frontend, backend and a layer between them and a layer between backend and database.有前端、后端和它们之间的一层以及后端和数据库之间的一层。 Frontend includes react.js , angular.js or anything else that is on browser.前端包括react.jsangular.js或浏览器上的任何其他内容。 Backend is used to take request from frontend, providing API's to frontend and ask for data from other API's or database.后端用于接受来自前端的请求,向前端提供 API 并从其他 API 或数据库请求数据。 Database includes sql , no-sql .数据库包括sqlno-sql

The error you are facing if of a NPM module mongodb-core.js .如果是 NPM 模块mongodb-core.js则您面临的错误。 Either it's not installed properly, or installed using wrong version of module which is not comparable with your node version, or wrong version of NPM, or module using another NPM module which is not installed.要么安装不正确,要么使用错误版本的模块安装,与您的节点版本无法比较,或使用错误版本的 NPM,或使用未安装的另一个 NPM 模块的模块。

The issue in your case is mongodb-core uses a module dns which is not been installed.您的问题是mongodb-core使用未安装的模块dns Try to install dns with npm i dns .尝试安装dnsnpm i dns or remove and install mongodb-core again.或再次删除并安装mongodb-core

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM