0

I am trying to implement elasticsearch.js in my project and when I added:

 var elasticsearch = require('elasticsearch');

It broke my project and said require is not defined. I did research and saw that I would have to use a library called require.js within my project but that is changing my whole project structure just for one script.

I wanted to see if anybody knows how to call an instance without using require:

 var elasticsearch = require('elasticsearch');
 var client = new elasticsearch.Client();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rethabile
  • 325
  • 3
  • 22

2 Answers2

2

You seem to be following the instructions for using elasticsearch in a node project or using a bundling system that supports CJS modules (like browserify or webpack). If you want a script that's for a browser-only project, see the Browser Builds page.

Note that at this time, they have this note:

These versions of the client are currently experimental.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Within the elasticsearch.angular.js file they have: var AngularConnector = require('./lib/connectors/angular');var Client = require('./lib/client'); – Rethabile Oct 07 '16 at 18:30
  • Did you see the sample project? https://github.com/spalger/elasticsearch-angular-example – Jacob Oct 08 '16 at 00:55
  • I'm not an Angular dev, but it seems like maybe Angular provides a `require` implementation. – Jacob Oct 08 '16 at 00:56
0

You're using a version that should be used in a node project or through a module loader/bundler. The require keyword is node specific, the browser has no idea what to do with it. Require.js would help, you can also install Rollup or Webpack which would bundle the CJS (require) dependencies and your code into one file.

Or to be simple just go to https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html as Jacob said

Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
Igor
  • 1