Bill
Bill

Reputation: 5150

ES6: Module '"mongoose"' has no default export

Using ES6 Imports & Exports I feel like I should be able to declare the import as import mongoose, { Schema, Document } from 'mongoose'; but I get the error Module '"mongoose"' has no default export.

The below does work but it clearly isn't the right way to go about this import. The export default id like to remove too.

import * as mongoose from 'mongoose';
import { Schema, Document } from 'mongoose';

export interface IUser extends Document {
  email: string;
  password: string;
}

const UserSchema: Schema = new Schema({
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true }
});

export default mongoose.model<IUser>('User', UserSchema);

And then im using it with

import UserModel, { IUser } from '../models/example'
import * as bcrypt from 'bcrypt';

class User {
  static register = async (req: Request, res: Response) => {
    const email = req.body.email;
    const password = req.body.password;
    const alreadyRegistered = await UserModel.findOne({email}).exec();
    if (!alreadyRegistered) {
      const hashedPassword = await bcrypt.hash(password,10);
        if (!hashedPassword) {
          res.status(500).send({ message: "Failed to encrypt your password" });
        } else {
          const user = new UserModel(<IUser>{email:email, password:hashedPassword});
          const saved = await user.save();
          if (!saved) {
            res.status(500).send({ message: "Failed to register you" });
          } else {
            res.status(200).send({ message: "You are now registered" });
          }
        }
    } else {
      res.status(400).send({ message: "You have already registered" });
    }
  };
}

export {User} 

Upvotes: 12

Views: 10758

Answers (6)

Milan Toncic
Milan Toncic

Reputation: 85

Make sure that you have "allowSyntheticDefaultImports": true, in tsconfig.json file.

Reference: https://github.com/microsoft/TypeScript-React-Starter/issues/8#issuecomment-301265017

Upvotes: 1

TheKido
TheKido

Reputation: 109

import { model, Schema } from 'mongoose';
...
export default model<IUser>('User', UserSchema);

Upvotes: 0

EL TEGANI MOHAMED
EL TEGANI MOHAMED

Reputation: 1898

I Have the same issue I noticed that I missed the tsconfig.json

Upvotes: 0

natac
natac

Reputation: 342

I came across this issue and fixed it by adding the following to tsconfig.json

"esModuleInterop": true

Then I could import as normal import mongoose from 'mongoose'

Upvotes: 14

Matus Dubrava
Matus Dubrava

Reputation: 14462

I am not sure how you are compiling the typescript but it should work just fine with the defaults set in tsconfig.json file.

  • generate tsconfig.json in your project's root directory by running tsc --init (as mentioned, you can use defaults for this to work with target set even to es5)

  • now you can run tsc -w or ts-node index.ts or however you prefer to build/run your code

Note that the index.ts file must be either in your project's root or you will need to specify the path in the tsconfig.json (the rootDir options which is commented out by default and uses path relative to the tsconfig file).

Also note that if you are running tsc -w or some other command that compiles/runs the code from some other directory than the one where tsconfig.json is located, you will need to provide a path to it, i.e. tsc -p path_to_config -w

Upvotes: 0

Gaurav
Gaurav

Reputation: 116

Try This

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true }
});


module.exports = mongoose.model('User', UserSchema);

2nd Way -

import * as mongoose from 'mongoose';

type UserSchema = IUser & mongoose.Document;

const User = mongoose.model<UserSchema>('User', new mongoose.Schema({
        email  : { type: String, required: true, unique: true },
          password: { type: String, required: true }
    }));
export = User;

Upvotes: 8

Related Questions