vokir
@vokir
Trainee Front-End Developer

Почему выбивает ошибки при подключении MongoDB?

Всем привет. У меня такая проблема. При импорте файла в котором находится класс с функциями для базы данных.
Я установил npm i mongodb. Но показывает куча ошибок.
ERROR in ./node_modules/mongodb/lib/cursor/abstract_cursor.js 7:17-34
Module not found: Error: Can't resolve 'stream' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib\cursor'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

ERROR in ./node_modules/mongodb/lib/gridfs/download.js 7:17-34
Module not found: Error: Can't resolve 'stream' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib\gridfs'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

ERROR in ./node_modules/mongodb/lib/gridfs/upload.js 7:17-34
Module not found: Error: Can't resolve 'stream' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib\gridfs'

ERROR in ./node_modules/mongodb/lib/sdam/srv_polling.js 7:12-26
Module not found: Error: Can't resolve 'dns' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib\sdam'

ERROR in ./node_modules/mongodb/lib/sdam/srv_polling.js 8:17-34
Module not found: Error: Can't resolve 'timers' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib\sdam'

ERROR in ./node_modules/mongodb/lib/sdam/topology.js 9:15-30
Module not found: Error: Can't resolve 'util' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib\sdam'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
        - install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "util": false }

ERROR in ./node_modules/mongodb/lib/utils.js 8:15-32
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
        - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "crypto": false }

ERROR in ./node_modules/mongodb/lib/utils.js 9:11-24
Module not found: Error: Can't resolve 'os' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "os": require.resolve("os-browserify/browser") }'
        - install 'os-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "os": false }

ERROR in ./node_modules/mongodb/lib/utils.js 10:14-28
Module not found: Error: Can't resolve 'url' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\mongodb\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "url": require.resolve("url/") }'
        - install 'url'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "url": false }

ERROR in ./node_modules/saslprep/lib/memory-code-points.js 3:11-24
Module not found: Error: Can't resolve 'fs' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\saslprep\lib'

ERROR in ./node_modules/saslprep/lib/memory-code-points.js 4:13-28
Module not found: Error: Can't resolve 'path' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\saslprep\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
        - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "path": false }

ERROR in ./node_modules/socks/build/client/socksclient.js 35:12-26
Module not found: Error: Can't resolve 'net' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\socks\build\client'

ERROR in ./node_modules/socks/build/common/helpers.js 9:15-32
Module not found: Error: Can't resolve 'stream' in 'C:\Users\dimay\Desktop\KP\tomato\node_modules\socks\build\common'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

Код файла который экспортирую
const {MongoClient} = require('mongodb');

const client  = new MongoClient('mongodb+srv://dimay:smUCZkYrknU6z@cluster0.swkj6bi.mongodb.net/Tomato?retryWrites=true&w=majority');

class DataBase {

    static async addNewUser(email, password){
        try{
            await   client.connect();
            console.log('OK');
            const users = client.db().collection('users');
            await users.insertOne({email:email, password: password});
        }catch (e){
            console.log(e);
        }
        
    }

    static async checkUserEmail(email){
        try{
            await   client.connect();
            console.log('OK');
            const users = client.db().collection('users');
            const user = await users.findOne({email: email});
            if(user===null){
                return [false]
            }else{
                console.log(user);
                return [true, user]
            }
        }catch (e){
            console.log(e);
        }
       
        
    }
}
export default DataBase


А вот код куда я его импортирую.
import './login.css';
import DataBase from '../../services/mongo.js';
import {useState} from 'react'

function Login() {
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const checkValidUser = async () =>{
   let result = await DataBase.checkUserEmail(email);
    if(!result[0]){
      console.log('Unknow email');
    }else{
      if(password === result[1]){
        console.log('ok')
      }else{
        console.log('wrong password')
      }
    }
  }
  return (
    <section className='login'>
      <div className="container">
        <form className="login_inputs" onSubmit={checkValidUser}>
          <label className="label__item" for="email">Enter e-mail</label>
          <input onChange={(e)=>setEmail(e.target.value)} className="inputs__item_log" type="text" placeholder='E-mail' name="email"/>
          <label className="label__item" for="password">Enter your password</label>
          <input onChange={(e)=>setPassword(e.target.value)} className="inputs__item_log" type="text" placeholder='Password' name='password'/>
          <button className='btn-login' >Login</button>
        </form>
      </div>
    </section>
  );
}

export default Login;


pachage.json
{
  "name": "tomato",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "mongodb": "^4.10.0",
    "mongodb-client-encryption": "^2.3.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.4.2",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "externals": ["mongodb-client-encryption"],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
  • Вопрос задан
  • 137 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы