Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR in ...lib/utils.js Module not found: Error: Can't resolve 'path' in '.../node_modules/elasticsearch/src/lib' #661

Closed
Son-Lam opened this issue May 14, 2018 · 18 comments

Comments

@Son-Lam
Copy link

Son-Lam commented May 14, 2018

Hello,

I am first time Elasticsearch user. I am trying to use it in my Angular 6 project (Angular 6, Rxjs 6, Node 10.1.0). I am getting this error while compiling:

ERROR in ./node_modules/elasticsearch/src/lib/utils.js
Module not found: Error: Can't resolve 'path' in '/.../node_modules/elasticsearch/src/lib'.

Could you help to resolve it.

Best Regards,

@YoannBureau
Copy link

YoannBureau commented May 14, 2018

Got the same problem here, pretty curious how to solve this.
FYI was working like a charm with Angular 5, just updated the Angular libs to 6 and then the problem came in.

@spalger
Copy link
Contributor

spalger commented May 14, 2018

Do either of you have time to create a reproduction project that I can use to debug? I'm unfamiliar with Angular 2+ and would take a while to get to the point of debugging this.

@Son-Lam
Copy link
Author

Son-Lam commented May 14, 2018

poc-elasticsearch.zip

Thanks @spalger ,

I attached here a short Angular 6 project. Here is the instruction how to re-produce the issue:

  1. Install NodeJs 10.1.0
  2. Install Angular CLI by running: npm install -g @angular/cli
  3. extract the attached zip file
  4. cd poc-elasticsearch
  5. npm install
  6. ng serve --open

You will get this log in command line:

Date: 2018-05-14T18:02:04.806Z
Hash: 65e95cc0b40537195983
Time: 6149ms
chunk {main} main.js, main.js.map (main) 14 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 227 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 5.4 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 15.6 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 5.55 MB [initial] [rendered]

ERROR in ./node_modules/elasticsearch/src/lib/utils.js
Module not found: Error: Can't resolve 'path' in '/Users/solam/workspaces/pocs/poc-elasticsearch/node_modules/elastic

Please let me know if you see any problem.

Thanks a lot,

Son Lam.

@YoannBureau
Copy link

YoannBureau commented May 14, 2018

@spalger sure.

BTW I just found the solution, it is a little bit weird.
So my elasticsearch lib import was made by require:
var elasticsearch = require('elasticsearch');
This will trigger the following error at compile time: Module not found: Error: Can't resolve 'path'

Fix: use UMD import syntax instead:
import * as elasticsearch from 'elasticsearch';
It will work like a charm

EDIT: Nope, does not work. See comments below

I just have created a sample project, you can download it until 30 days: https://ufile.io/w7m0i

In order to run it locally, you need to do the following:

  1. install angular cli globally: npm i -g @angular/cli
  2. in the project folder, install nppm deps: npm i
  3. run the project by the following cli command: ng serve

The file you are interested in is src/app/app.component.ts. I've put the two import versions, uncomment the one you are interested in.

@YoannBureau
Copy link

@Son-lan that is what we call a message percussion :)
See my comment for the solution.

@Son-Lam
Copy link
Author

Son-Lam commented May 14, 2018

poc-elasticsearch_2.zip

@spalger ,

👍 :-) it is good that it takes only few minutes to create the test case.

I tried your solution but it does not seem to fix my problem. My code is very simple:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import * as elasticsearch from 'elasticsearch';

@Injectable()
export class ElasticSearchService {
    private _client: elasticsearch.Client;

    constructor() {
        if (!this._client) this._connect();
    }

    private _connect() {
        this._client = new elasticsearch.Client({
            host: 'http://localhost:9200',
            log: 'trace'
        });
    }

    search(value): any {
        if (value) {
            console.log(value);
            return this._client.search({
                index: 'blog',
                q: `title:${value}`
            });
        } else {
            return Promise.resolve({});
        }
    }

    addToIndex(value): any {
        return this._client.create(value);
    }

    isAvailable(): any {
        const pp: elasticsearch.PingParams = {
            requestTimeout: Infinity,
            body: 'Hello elasticsearch!'
        };

        return this._client.ping(pp);
    }
}

Could take another look?

Thanks,

Son Lam.

@YoannBureau
Copy link

@Son-Lam are you sure you don't have any other references to elasticsearch somewhere else in your code? I've copy/pasted your service in my project and it does compile:
image

@Son-Lam
Copy link
Author

Son-Lam commented May 15, 2018

@spalger , Thanks a lot for looking into it.

Could you check what version of NodeJs do you have?

That the only place I have a reference to elasticsearch. If you extract the attached "poc-elasticsearch_2.zip" file above.

Thanks,

@YoannBureau
Copy link

I am sorry, it compiled, but I was not importing the service in my providers. When I did then the problem went back again.

@Son-Lam
Copy link
Author

Son-Lam commented May 15, 2018

It is great. We are on the same page now. I hope @spalger could help to solve this issues faster.

Thanks a lot.

@YoannBureau
Copy link

@Son-Lam looks like it's a cli problem, not only with path. Many people are having troubles right now: angular/angular-cli#10681

I tried this solution but didn't worked: angular/angular-cli#10681 (comment)

@Son-Lam
Copy link
Author

Son-Lam commented May 15, 2018

@YoannBureau , I have upgraded to version 15.0.0 and the error went away 👍 .

Could you try and confirm it has been fixed for you too?

@Son-Lam
Copy link
Author

Son-Lam commented May 15, 2018

Solution: upgrade to version 15.0.0.

@Son-Lam Son-Lam closed this as completed May 15, 2018
@bjdivakar
Copy link

@Son-Lam which package you upgraded to 15.0.0

@praveenwala
Copy link

Upgrading to 15.0.0 doesn't solve my problem. Any Other Solution? @Son-Lam

screen shot 2018-07-06 at 5 13 01 pm

@generalomosco
Copy link

generalomosco commented Jul 26, 2018

Install the require dependencies using this line below
npm i --save util events lodash url querystring util events lodash url querystring http https agentkeepalive zlib

@niazlake
Copy link

niazlake commented Oct 3, 2018

Install the require dependencies using this line below
npm i --save util events lodash url querystring util events lodash url querystring http https agentkeepalive zlib

this didnt help me

@demisx
Copy link

demisx commented Oct 6, 2019

Installing deps made a couple of similar errors to go away for me, but not all of them. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants