mongoose: Bug: `Named export 'Schema' not found.` (TypeScript)
Do you want to request a feature or report a bug?
bug
What is the current behavior?
When importing mongoose using import { Schema, model } from 'mongoose'
,
TypeScript shows no errors and intellisense works just fine, but
The program errors out with this :
import { Schema, model, connect } from 'mongoose';
^^^^^^
SyntaxError: Named export 'Schema' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'mongoose';
const { Schema, model, connect } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:179:5)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
at async handleMainPromise (node:internal/modules/run_main:63:12)
If I replace the importing line with import mongoose from 'mongoose'
as suggested in the error message, TypeScript shows Module "mongoose" has no default export
, while the code runs just fine
And if I replace it with import * as mongoose from 'mongoose'
instead, it produces a whole new set of errors, basically it is not at all equivalent to import {...} from 'mongoose'
so, steps to reproduce.
- inside package.json:
"type": "module"
- tsconfig.json:
{
"compilerOptions": {
"moduleResolution": "node",
"target": "ESNext"
}
}
- copy the code from the official tutorial at https://mongoosejs.com/docs/typescript.html
- compile
- run
What is the expected behavior?
Running without errors (“bill@initech.com” printed in the console) OR At least throwing errors on the TypeScript side that the imports do not exist
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.
Mongoose: v5.13.4 MongoDB: v4.4.6 NodeJS: v16.5.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (1 by maintainers)
I am facing this exact problem, even the example breaks giving me:
Untyped function calls may not accept type arguments