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
frontendmonster opened this Issue Nov 27, 2017 · 6 comments

Comments

Projects
None yet
6 participants
@frontendmonster

frontendmonster 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

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Nov 27, 2017

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.

Collaborator

babel-bot commented Nov 27, 2017

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

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Dec 1, 2017

Member

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

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

@frontendmonster

This comment has been minimized.

Show comment
Hide comment
@frontendmonster

frontendmonster Dec 1, 2017

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

frontendmonster commented Dec 1, 2017

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

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Dec 1, 2017

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.

Member

loganfsmyth commented Dec 1, 2017

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.

@xtuc xtuc added the area: modules label Dec 5, 2017

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Dec 26, 2017

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

Member

nicolo-ribaudo commented Dec 26, 2017

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

This comment has been minimized.

Show comment
Hide comment
@vkarpov15

vkarpov15 Dec 27, 2017

@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.

vkarpov15 commented Dec 27, 2017

@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.

@loganfsmyth loganfsmyth closed this Mar 5, 2018

@lock lock bot added the outdated 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.