Export/Import Error?


#1

I have this code at the bottom of a file called ‘/imports/api/db-connectors.js’:

const Instant_messages = db.models.instant_messages;
const a = 100; //<== breakpoint here shows an object with fields, etc., stored in Instant_messages
export {Instant_messages };

Then at the top of another file, I have this import statement:

import {Instant_messages } from ‘/imports/api/db-connectors’;
import { Kind } from ‘graphql/language’;
const b = 100; <==breakpoint here shows no such local var as Instant_messages

…there is no local variable named Instant_messages available to reference.

What am I missing?


#2

Does it work if you use relative paths`? And can any auto-completion in your IDE help you verify that the paths and export/import variable names are correct?


#3

I thought that was a relative path. :slight_smile: Not so?

The path seems to be finding the correct file. I got it working like this:

import * as connectors from '/imports/api/db-connectors';
const check = connectors.Instant_messages;

But I’d love to find out how I could do it correctly the other way too, as that seems to be more common.


#4

Great. That trick has also helped me quite often.
Let’s say the file you import into is placed in the /imports folder - then the relative path in the import statement would be be ./api/do-connectors

When you start the path with a / it will be from the root of your project.


#5

What IDE auto completes the imports for you?


#6

I mostly use vscode and previously webstorm. They can both do it.


#7

I’m using Webstorm! (more characters to get to 20)


#8

The relevant folder layout is:

imports ƒ
–api ƒ
----db-connectors.js
----resolvers.js //<== this is the file importing from db-connectors.js


#9

I use VS code too – what’s the plugin name?


#10

If I try:

import * as connectors from './api/do-connectors';

…I get a module not found error.

Is that not the correct relative path?


#11

You don’t need any.

If you open the whole folder that contain your project it should work. For instance, when I press control (windows) or command (mac) and hover the path in the import statements I’m able to use them as links.


#12

based on your folder layout the correct relative path should be ./db-connectors


#13

The path works. Thanks!

To return to the original topic of this post, using:

import {Instant_messages} from './db-connectors';

…still does not create a local variable named Instant_messages. Isn’t that odd?


#14

try: export Instant_messages;