francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
103 stars 24 forks source link

official tutorial sample code problem #74

Closed aaronyu6 closed 3 years ago

aaronyu6 commented 3 years ago

follow your official tutorial/readme:

problem context: using nestjs, follow nestjs offical guide install mongoose package "mongoose": { "version": "5.13.3", ... }

how to recreate problem: if the user.ts use code as below: import mongoose from "mongoose";

run: npx mtgen it return error:

..$ npx mtgen Generating mongoose typescript definitions... ! TypeError: Cannot read property 'model' of undefined

related code: export const User = mongoose.model("User", UserSchema); it means the mongoose is imported incorrectly.

How to correct the error: if the user.ts use import * as mongoose from "mongoose"; run: npx mtgen work successfully.

I search that: import as mongoose from "mongoose"; is the standard method to load mongoose therefore, 1.could you amend tutorial a bit, change from: import mongoose from "mongoose"; change to: import as mongoose from "mongoose"; to make it easier for other users to avoid the same error? 2.maybe the import mongoose from "mongoose"; works on some other condition, if you know, kindly remind in tutorial/readme in what condition use this way.

francescov1 commented 3 years ago

Hey @aaronyu6,

According to Mongoose README, import mongoose from "mongoose" is the correct way with ES6 syntax, see here. Make sure you've installed mongoose prior to following the tutorial. If you have then there may be some other issue with your project or environment

aaronyu6 commented 3 years ago

this problem does not happen with JS ES6, but it occur with typescript.

some related post from others node.js - Mongoose Import for TypeScript Doesn't Work - Stack Overflow https://stackoverflow.com/questions/49001632/mongoose-import-for-typescript-doesnt-work

This:

import mongoose from 'mongoose'

worked for me after running:

npm install mongoose @types/mongoose --save

Way more detailed explanation on why this works, here. Share Improve this answer Follow answered Jul 2 '20 at 2:15 Lucio Mollinedo 1,61211 gold badge2222 silver badges2626 bronze badges Add a comment 1

The solution to this problem is to comment the below line in your tsconfig.json file found in your project's base directory, just comment this damn line

"esModuleInterop":true

another post is: node.js - ES6: Module '"mongoose"' has no default export - Stack Overflow https://stackoverflow.com/questions/57635797/es6-module-mongoose-has-no-default-export

anyway, in my context, only import * as mongoose from "mongoose" work, what is in your tutorial(import mongoose from "mongoose") does not work. there must be a reason behind it.

aaronyu6 commented 3 years ago

this post has some information seems very alike to this issue import fails with 'no default export' · Issue #8 · microsoft/TypeScript-React-Starter https://github.com/microsoft/TypeScript-React-Starter/issues/8

aaronyu6 commented 3 years ago

I guess the tsconfig.json has some relation to this problem, below is the tsconfig.json file content

aaronyu6 commented 3 years ago

{ "compilerOptions": { "module": "commonjs", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", "sourceMap": true, "outDir": "./dist", "baseUrl": "./", "incremental": true } }

francescov1 commented 3 years ago

Hi @aaronyu6 ,

I'm not sure why the import doesn't work for you, but it's directly in the Mongoose README and the same problem does not occur to other users, so I dont think this is an issue relevant to mongoose-tsgen. Additionally, @types/mongoose should not be installed with mongoose as the types will conflict with the new Mongoose types included directly in the library.

Closing this.