Edgar
Edgar

Reputation: 6856

SyntaxError: The requested module does not provide an export named 'connect'

when i use require commonjs everything is working

const { Schema, connect } = require("mongoose");

connect("mongodb://localhost:27017/usersdb", {
  useNewUrlParser: true
});

but when i use import es6 i get error

import { connect } from "mongoose";

connect(
  "mongodb://localhost:27017/usersdb",
  {
    useNewUrlParser: true
  }
);

SyntaxError: The requested module >'file:///C:/Users/Rich%20Warrior/Desktop/mon0022/node_modules/mongoose/index>.js' does not provide an export named 'connect'

and if you write like that

import * as mongoose from "mongoose";

mongoose.connect("mongodb://localhost:27017/usersdb", {
  useNewUrlParser: true
});

TypeError: mongoose.connect is not a function

package.json

{
  "name": "dur",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon -r esm src/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "esm": "^3.2.25",
    "express": "^4.17.1",
    "i": "^0.3.6",
    "mongoose": "^5.7.8",
    "nodemon": "^1.19.4"
  }
}

I want to use the es6 module.Is there a way to use es6 modules with mongoose without getting an error?

Upvotes: 1

Views: 7131

Answers (1)

Shihab
Shihab

Reputation: 2679

NodeJS doesn't support it yet. You will have to use something like bable. Check this out from NodeJS doc as well.

Upvotes: 0

Related Questions