[1.3.1] Strange import behavior

Hi there!

I have a folder /app/libs/schemas with all my schemas in it. I also have an index.js file in there, which looks like this:

import { schema as Address } from './address';
// Some more imports ...
import { schema as User } from './user';

export {
  Address,
  // Some more exports ...
  User
};

Now the strange thing: When I try to import { Address }, it doesn’t work. Importing * as schemas works though,

> import { Address } from '/app/lib/schemas'
> Address
ReferenceError: Address is not defined
> import * as schemas from '/app/lib/schemas'
> schemas.Address
{ [Function: Class] /* ... */ }

What am I doing wrong?

Does this work as expected in a file as opposed to meteor shell ?

Same thing, it actually didn’t work in a file so I tried the shell.

Does it work if you use:

No, I think that’s not a valid syntax. Surprisingly I can do something like

import { a as b } from './a';
export { b };

But I want to keep the names. @benjamn, this is a tricky one. Do you have an idea?

I just answered a similar question!

Copied here:

Unfortunately, the semantics of import are not really compatible with meteor shell or any shell-like REPL environment: https://github.com/meteor/meteor/issues/6271#issuecomment-187999135

Because symbols imported using import are mutable bindings, a compiler for import statements needs the complete picture: not only what you imported and what module it came from, but where those symbols are used in the rest of your code. A REPL divorces the import statement from the symbol usage, making the compiler’s job impossible.

In short, when you’re using meteor shell, you need to use require.

Hi, thanks for your response! But this problem isn’t just about the meteor shell, values are undefined in the files as well.

What do the export statements look like in address.js and user.js?

They basically look like this:

import { Schema } from '/app/lib/helpers/schema';

const schema = Schema.create({});

export { schema };

I’m not exactly sure what’s going on with the current code, but you could try a slightly different syntax in index.js:

export { schema as Address } from './address';
export { schema as User } from './user';

In address.js you might also try this single-line declare-and-export syntax:

export const schema = Schema.create({});

Using the alternative syntax in index.js seems to fix it.

export { schema as Address } from './address':

Should I still open an issue? This behavior seems to be a bug.

No, it doesn’t. The issue then happens for another file. Maybe it’s because the files are in the same folder? I’m trying to use this import {} from '/app/lib/schemas inside of files, which are schemas themselves and are imported into the index.js file. Might be an issue with the loading order?

Please do open a GitHub issue! If you have a small reproduction repo that you can publish, that would be wonderful.

Was this issue ever solved? I’m still having undefined packages when I try to import them