elasticsearch-js: TypeError: Cannot read property 'split' of undefined in node_modules/@elastic/elasticsearch/index.js:7

🐛 Bug Report

TypeError: Cannot read property ‘split’ of undefined in node_modules/@elastic/elasticsearch/index.js:7

To Reproduce

Steps to reproduce the behavior:

  • npx create-react-app test-app
  • cd test-app
  • npm install @elastic/elasticsearch
  • edit ./src/App.js to require the library
// ./src/App.js

import logo from './logo.svg';
import './App.css';

const { Client } = require('@elastic/elasticsearch')

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;
  • npm start
  • error reported int he browser:

TypeError: Cannot read property ‘split’ of undefined (anonymous function) node_modules/@elastic/elasticsearch/index.js:7

  • code snippet in the error:
   4 | 
   5 | 'use strict'
   6 | 
>  7 | const nodeMajor = Number(process.versions.node.split('.')[0])
   8 | 
   9 | const { EventEmitter } = require('events')
  10 | const { URL } = require('url')

Expected behavior

the module should be required without error

Debugging

I did console.log(process.version.node); in node_modules/@elastic/elasticsearch/index.js. It looks to me that process.versions.node is undefined.

Your Environment

  • node version: was v12.18.4 upgraded to v14.15.0 same issue
  • @elastic/elasticsearch version: 7.9.1
  • os: Mac
  • any other relevant information:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 5
  • Comments: 19 (3 by maintainers)

Most upvoted comments

npm modules should work also with client libs such as React or Vue.

@vikiival I don’t agree, some libraries can work in both environments, others can’t. It’s not safe connecting directly to Elasticsearch from the browser, as it will expose you to security risks, hence we do not support it. You can write a small node.js proxy for sending queries and make the browser communicate with your proxy.

Just to a comment to vote in favor of re-opening this issue and supporting browser applications. It is certainly possible to have a secure ES instance without the need of a proxy. In our case we use AWS’s Elasticsearch service along with AWS’s Cognito and IAM authentication to grant the client restricted access only to verified users.

In any case, It seems to me that the SDK should not be opinionated about how to secure the server.

It should also be noted that the legacy sdk did support browser environments.

3 years later and I still get notifications for this issue 🥺

image i have a error similar. But with me, I require ‘dotenv’ before config and this help me.

@vikiival I solved it reverting to the legacy client: https://www.npmjs.com/package/elasticsearch

@andreasanta Even if your cluster is properly configured, and you are using an ES user with very low permissions, an attacker could still send heavy queries and overload the cluster. Would you ever expose postgres/mongodb to the world?

@delvedor PostGres and MongoDB are read-write databases that can contain sensitive information. I would not expose them for security reason. But just the search endpoint of ElasticSearch is a perfect candidate for me for direct JS access. What’s the use of introducing a BE middleware that perform just request throttling and introducing unneeded latency? Maybe I’m missing something, but the comparison with DBs is a bit daring 😃

If recall correctly, I was able to hardcode a nodeMajor constant to get it to work in react. I eventually switched to using a node.js proxy between the react.js app and elasticsearch.

// in node_modules/@elastic/elasticsearch/index.js

// replaced this line 
const nodeMajor = Number(process.versions.node.split('.')[0])

// with something like this
const nodeMajor = 14.15.0

I can’t say that everything would work but it might be OK for a rapid prototype.

@delvedor I am also having this issue, but exposing an ElasticSearch cluster to the web does not really entail security risks if your indices are protected properly and your infra is properly configured. On the contrary, it allows fast low latency query from the front end when interrogating ES from the browser. Would you please re-open the issue? 🙇