Cannot import module with Elastic Search functions


#1

Hey all,

I’ve been struggling with a problem for far too long. It’s been troubling me off and on for a few weeks, and I can’t seem to find the solution.

I’m trying to import an elastic search functions into my /imports/api/*/methods.js files. It works absolutely fine in one instance, however, as soon as I try to import the same function elsewhere (another file); I get the following error from my browser console.

Uncaught Error: Cannot find module '/imports/api/elastic/server/elastic.js'

The function is being created and exported from /imports/api/elastic/server/elastic.js, however I’ve tried it locating elsewhere with no effect. Here is that file for reference…

import es from 'elasticsearch';
import { Meteor } from 'meteor/meteor';
import { Promise } from 'meteor/promise';

const es = new Elastic.Client({
 host: [{
   host: 'hidden-cluster-address-here',
   auth: Meteor.settings.ESCloudRW,
 },],
});

export function esIndexExists(indexName) {
 const promise = es.indices.exists({ index: indexName });
 return Promise.await(promise);
}

export function esCreateIndex(indexName) {
 const promise = es.indices.create({ index: indexName });
 return Promise.await(promise);
}

The ES client needs to be defined on the server, but should be able to be used anywhere, i.e. a shared client/server file like methods.js

This doesn’t appear to be an elasticsearch package issue, as I’ve not come across any other reference to it, however I’m only experiencing this problem with trying to import from this one file.

The error is thrown not by attempting to use the function multiple times, but by importing it. Maybe I’m encountering this error because I can’t reference the file more than once due to the presence of the ES client?

Importing any of those functions from the elastic.js file once is fine, additional imports in other files (even if different modules) causes the error.

The more I look at the problem, the more it seems like an issue with the Meteor build tool?

I’m all out of ideas. Help!

Cheers,

Jamie.

Sidenote: Locating all the methods in one file stops the error, but methods that previously ran cause incomprehensible errors in the terminal.


Resources/Blogs/Threads on elasticsearch and meteor?
#2

Nobody else encountered this? No one have any suggestions?

I raised the issue with the elastic search team, and they feel it’s a Meteor build tool issue (and I’m inclined to agree at this stage)


#3

Right, after way too much swearing and messing about; I finally found the issue…Meteor. :smiling_imp:

My issue was that the client was trying to import a file that can only exist server-side (due to ElasticSearch not working within a browser environment). That caused the build to fail big, rather than silently client-side whilst working server-side with the Cannot find module... error

I resolved it by defining my method inside a shared file, but specifying that it only be run on the server. Unfortunately I can’t use import so I have to fall back to using require. The code now looks like this:

import { Meteor } from 'meteor/meteor';
if (Meteor.isServer) {

  let client = require('/imports/api/elastic/server/client.js');

  Meteor.methods({
    addBike(bikeAttributes) {
      check(bikeAttributes, Object);

      // lots of other code here....

      // Insert in ES cluster
      client.esAddDocument('marketplace', 'bikes', bike._id, bike);
    },
  });
}

esAddDocument() is a function that I defined in the server-only file above (enforced by living within a /server folder. Meteor’s black box magic can be quite tiresome sometimes…