Desperate! How to import from outside the `/imports` directory

I have files in a /models/ directory off the root of the Meteor application. That directory is in .meteorignore so it’s not automatically loaded.

I can’t do import SomeModule from '/models/some-module.js' from a file in the /imports/ directory. In some-module.js, the error says that the export or import (at the top of the some-module.js file) is a Syntax Error.

In other words it is treating /models/ directory different than /imports/ directory. The same file in the /imports/ directory will import fine.

Please, if anybody knows how to fix this without moving all the files into /imports/ I would be grateful.

Not sure if it works, but you can try relative imports like this:

import someModule from '../models/some-module.js';

Hope that works… whats the reason for the file being in a different folder? Can’t you simply move it to imports?

Also in meteor v1.7 you can ditch the imports folder and specify the apps entry point in the package.json

Thank you so much for the response cloudspider. I’m trying to get this code out today so the help is greatly appreciated.

I tried the .. but unfortunately that didn’t work.

It’s possible to move to the imports folder, but I want to avoid it. The stuff in the models folder is a newer version of the stuff in the imports folder but they both need to co-exist (i.e. name collisions). I can put it into imports/models but I need to remap all the import statements to the new locations. I want to avoid it because the project is big.

Because Meteor takes a long time to reload and I needed faster iteration, the new libraries work with an RPC server which replaces meteor’s Meteor.call. I still use Meteor.publish though and when I tried to access the /models code I started seeing the failures.

Meteor 1.7 and the entry point in package.json sounds promising. I would assume then that Meteor would track all the directories such that they could be imported.

The problem here is that you’ve listed models/ in your .meteorignore, which means it won’t be compiled.

I’m assuming you put it in there to prevent it from being loaded eagerly?

If so, the easiest solution is to add a "meteor": {"mainModule": {...}} section to package.json and remove the models/ line from .meteorignore

1 Like

You are correct about why I added it to .meteorignore.

Unfortunately I’m running Meteor 1.5.4.2. I tried to upgrade but it broke so many things and includes a rather painful looking upgrade to the new babel (painful as I have a configuration that works just right with Webpack HMR and jest and to get those working with Meteor was painful).

I think it might be a day or more just to do the upgrade. Otherwise it would be the right solution but is probably the way to go in the long run.

I’m working on an interim solution but if you have any other suggestions, I’d be grateful. If you have a tip jar or favorite charity, please let me know. As the title says, I’m desperate and just having some avenues to think has been helpful. Not a lot of Meteor internals style knowledge out there.

Can you share with us why you don’t want it to eager load?

These are modules used to initialize objects and for access control like init.book. This initializes a book object and makes sure the user has the right permissions to access the book. It’s not like Meteor.publish or Meteor.method but rather functions you use inside them. In this case, I want to use it in Meteor.publish.

I’m also using these same modules in an rpc (remote procedure call) server I’ve created. This replaces Meteor.methods with something more flexible and reloads instantly. Since this code is standard JavaScript, I can run Jest tests against which makes the dev process faster. This is because tests run almost instantly while Meteor tests ran very slowly and the Jest UI is fantastic.

It was not intuitive to get authorization working against Meteor from plain node.js. While people talked about it I couldn’t find a working implementation. I found the correct algorithm through the Meteor source code.

Try meteor npm link.

Those instructions are specifically for Meteor 1.7.1/1.8+

It might be enough to fool the build system into building something in .meteorignore though, so worth a try?

Thanks for the suggestions,

Will meteor npm link work for deploy as well? What I mean is will it link to the absolute directory on my computer or will it link a directory relative to the current directory?

Don’t want to run into the issue in that it works on dev but breaks on deploy.