Home > OS >  I have Created a Whatsapp Clone using MERN Stack. Facing Errors in backEnd side
I have Created a Whatsapp Clone using MERN Stack. Facing Errors in backEnd side

Time:03-28

I have created a WhatsApp clone in the backend of Whatsapp clone i am facing following errors. when i run yarn start i am facing following errors in console.

yarn run v1.23.0-20200615.1913 $ node server.js node:internal/errors:464 ErrorCaptureStackTrace(err); ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'react' imported from E:\Whatapp clone\whatsapp-backend\server.js
    at new NodeError (node:internal/errors:371:5)
    at packageResolve (node:internal/modules/esm/resolve:884:9)
    at moduleResolve (node:internal/modules/esm/resolve:929:18)
    at defaultResolve (node:internal/modules/esm/resolve:1044:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

My main file Server.js (It is the main file )

// Import

import react from 'react';
import express from "express";
import mongoose from "mongoose";
import message from './dbMessages.js';
import Pusher from "pusher";
import cors from "cors";
import {useState} from "react";
// import axios from "axios";

// app config
const app = express();
const port = process.env.PORT || 90000;

// Pusher code
const pusher = new Pusher({
    appId: "1366318",
    key: "eb9c7412499f6e7b4c5f",
    secret: "6903a1ffa86fcb2c3ddd",
    cluster: "ap2",
    useTLS: true
  });

// middle ware
app.use(express.json());
app.use(cors());
app.use((req,res, next ) => {
    res.setHeader("Access-Control-Allow-Origin" ,"*");
    res.setHeader( "Access-Control-Allow-Origin" ,"*");
    next();
});
// DB Config
const connection_url='mongodb srv://whatsapp:[email protected]/whatsapp?retryWrites=true&w=majority';

mongoose.connect(connection_url,{
    useCreateIndex: true,
    useNewUrlParser: true,
    useUnifedTopology: true
})


const db = mongoose.connection;

db.once("open",()=>{
    console.log("db connected");
    // message contents is from mongodb database
    const msgCollection = msgCollection('whatsapp'); //collection name in mongodb messagecontents
    // messagecontents changing to whatsapp
    const changeStream = msgCollection.watch();
    // Change Stream  function
    changeStream.on("change", (change) => 
    {
        // this is when we troggling pusher
        console.log('A changed is occur',change);

        if (change.operationType === 'insert'){
            const messageDetails = change.fullDocument;
            pusher.triggle('message', 'insert', {
                name:messageDetails.user,
                message: messageDetails.message,
                timeStamp: messageDetails.timeStamp
            });
        } else {
            console.log('Error Troggling pusher');
        }
    });
});
// api router
app.get('/',(req,res) => {
    res.status(200).send("Hello World");
})

app.get('/messages/sync' , (req,res)=>{
    Messages.find((err,data)=>{
        if (err) {
            res.status(500).send(err)
        }
         else {
             res.status(200).send(data)
         }
    })
})


app.post('/api/messages/new', (res,req)=>{
    const dbMessage = req.body

    message.create(dbMessage, (err,data)=>{
        if (err){
            res.status(500).send(err)
        }
        else {
            res.status(201).send(`new number created: \n ${data}`)
        }

    })
})
// listen
app.listen(port, () => console.log(`listen on local host ${port}`));

MY schema (dbMessages.js)

import mongoose from "mongoose";

const whatsappSchema = mongoose.Schema({
    message:String,
    name:String,
    timestamp:String
});

export default mongoose.model('whatsapp', whatsappSchema);
// messagecontent changing to whatsapp

My Package.Json

{
  "name": "whatsapp-backend",
  "version": "1.0.0",
  "discription": "",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "nodemon":"nodemon --experimental-modules --es-module-specifier-resolution=node "
  },
  "keywords": [],
  "author": "zain",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.17.3",
    "express_js": "^1.0.0",
    "mongoose": "^6.2.5",
    "nodemon": "^2.0.15",
    "pusher": "^5.0.1",
    "pusher-js": "^7.0.6"
  }
}

CodePudding user response:

Remove this line and run:

import react from 'react';

CodePudding user response:

remove import react from 'react'; from your Server.js file because the server-side doesn't know what react is.

  • Related