Home > other >  Nodejs database fails for both mongoose and cloud mongodb with "No valid exports main found for
Nodejs database fails for both mongoose and cloud mongodb with "No valid exports main found for

Time:10-08

In a barebones node.js app I am unable to connect to either a local mongoose database or a cloud mongoDB database.

Here is the error:

Error: No valid exports main found for 'D:\Documents\code\node-api\node_modules\mongodb-connec
tion-string-url'
←[90m    at resolveExportsTarget (internal/modules/cjs/loader.js:625:9)←[39m
←[90m    at applyExports (internal/modules/cjs/loader.js:502:14)←[39m
←[90m    at resolveExports (internal/modules/cjs/loader.js:551:12)←[39m
←[90m    at Function.Module._findPath (internal/modules/cjs/loader.js:657:22)←[3
9m
←[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:960
:27)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:855:27)←[39m
←[90m    at Module.require (internal/modules/cjs/loader.js:1033:19)←[39m
←[90m    at require (internal/modules/cjs/helpers.js:72:18)←[39m
    at Object.<anonymous> (D:\Documents\code\node-api\node_modules\←[4mmongodb←[24m\lib\connec
tion_string.js:6:41)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1144:30)←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m
}
[nodemon] app crashed - waiting for file changes before starting...

Here are 2 code snippets that both yield the error: local:

const mongoose = require('mongoose');

module.exports = async () => {
    try{
        await mongoose.connect(process.env.DB_URL, {useNewUrlParser: true});
        console.log("Database connected");
    }catch(error)
    {
        console.log("error: ", error);
        throw new Error(error);
    }
   
}

and cloud:

const { MongoClient } = require('mongodb');
const uri = "mongodb srv://user1:<[mypasswordgoeshere]>@cluster0.vl2la.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

There are many questions here with a similar error, and I've followed the steps for as many as possible, including reinstalling the packages, delete node_modules and reinstalling, installing autoprefixer, and downgrading versions of various packages. It's important to note that I'm on Windows 7 and therefore cannot update my node.js version any further.

Here is my packages.json file:

{
  "name": "node-api",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "autoprefixer": "^9.8.0",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "mongoose": "^6.0.8"
  },
  "devDependencies": {
    "nodemon": "^2.0.13"
  }
}

CodePudding user response:

For some reason, starting the server in Node.js terminal produces this error. Doing everything from Windows 7 cmd works perfectly without this error.

  • Related