Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing both named and default from same file not working. #6915

Closed
ASafaeirad opened this issue Nov 27, 2017 · 6 comments
Closed

Importing both named and default from same file not working. #6915

ASafaeirad opened this issue Nov 27, 2017 · 6 comments
Labels
area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@ASafaeirad
Copy link

ASafaeirad commented Nov 27, 2017

bug report:
after upgrading to v7.0, ES6 import seems weird.
importing both named and default from file seems not working right.
named imports become undefined when adding default import on same file.

Input Code

import mongoose, {Schema} from 'mongoose';
console.log(Schema); // undefined

but it works when removing default import.

import {Schema} from 'mongoose';
console.log(Schema) // object

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{"presets": ["@babel/preset-env"] }

Expected Behavior

import mongoose from 'mongoose';
import {Schema} from 'mongoose';
console.log(Schema); // object

Current Behavior

import mongoose from 'mongoose';
import {Schema} from 'mongoose';
console.log(Schema); // undefiend

Your Environment

software version(s)
Babel 7.0.0-beta.32
node 9.2.0
npm 5.5.1
mongoose 4.13.5
Operating System Win10-x64

Edit

it just happens with mongoose. but when i downgrade babel it fixed.

@babel-bot
Copy link
Collaborator

Hey @frontendmonster! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Dec 1, 2017

mongoose has only a default export, not named exports (which are the equivalent of doing module.exports.Schema = Schema):
https://github.com/Automattic/mongoose/blob/master/lib/index.js#L860

@ASafaeirad
Copy link
Author

@nicolo-ribaudo Ok, but why import { Schema } from 'mongoose'; works?

@loganfsmyth
Copy link
Member

Hmm, this is a pain. The mongoose module puts .Schema on the prototype of the module.exports object.

So if you only have a named export, we don't bother using our interopRequireWildcard helper, but if you include the default, we do. The helper ignores prototype properties on module.exports, but the non-helper cases does access them.

We can leave this open so we can track it, but my recommendation would absolutely be to do

import mongoose from 'mongoose';
console.log(mongoose.Schema);

when you need the schema. ES6 import statements are not destructuring statements. This behavior may yet change more for Babel 7, we haven't fully decided yet.

@nicolo-ribaudo
Copy link
Member

I'm tempted to mark this as won't fix/by design, since module.exports = new Mongoose, which mongoose uses [1], is the equivalent of export default new Mongoose. Thus, import { Schema } from "mongoose" is wrong.

cc @vkarpov15 (I'm pinging you because by looking at mongoose's commits you are the main maintainer).
What do you think about this?

[1] https://github.com/Automattic/mongoose/blob/6da0c525f5ab080a78631278cdb0d7d435ca976d/lib/index.js#L863

@vkarpov15
Copy link

@nicolo-ribaudo I agree, statements like import * from 'mongoose' and import {model} from 'mongoose' do not work with mongoose because of how the lib is structured. I opened up an issue on the mongoose repo (Automattic/mongoose#5940) to make a note of this on the readme.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

6 participants