Elasticsearch : Uncaught ReferenceError: elasticsearch is not defined

1.7k Views Asked by At

I am trying to create a NodeJS application using Elasticsearch

here is my code

<!DOCTYPE html>
<script src="../bower_components/elasticsearch/elasticsearch.js"></script>
<script>
function esPinger() {
    var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

client.ping({
  // ping usually has a 3000ms timeout
  requestTimeout: Infinity,

  // undocumented params are appended to the query string
  hello: "elasticsearch!"
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
    alert("YAY");
  }
});
}
</script>
<html>

  <head>
    <title>NodeJS Starter Application</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="stylesheets/style.css">
  </head>

  <body>

    <center>
    <button onclick="esPinger()">Ping ES</button>
    </center>
  </body>

</html>

i installed the elasticsearch client as

npm install elasticsearch 

and also

bower install elasticsearch 

in my project folder, i can see that there is a bower_componenets folder that has all of the .js files for elasticsearch, including elasticsearch.js

now, when i run my page and click on the button, i get

Uncaught ReferenceError: elasticsearch is not defined

any idea why? am i missing some configuration?

for reference, here is what my project structure looks like

enter image description here

Edit

Here is what i see in the inspect element console

enter image description here

how can it not get the library!

1

There are 1 best solutions below

0
HOTAM SINGH On

Node.JS is a "server-side" technology, not a "browser" technology.
Thus, Node-specific calls, like:

var elasticsearch = require('elasticsearch');

do not work in the browser.