Using the http-client library in Webpack Starter

Enonic version: 7.0.2
OS: Win10

I’m having some difficulties getting the https://github.com/enonic/lib-http-client to work with a project based on the webpack starter app.

I’ve added include "com.enonic.lib:lib-http-client:2.0.0" to the dependiencies in build.gradle

When importing with const httpClient = require('/lib/http-client');
I get the build error:

ERROR in ./lib/observe/observe.es6
Module not found: Error: Can't resolve '/lib/http-client' in 'C:\Users\Martin\work\internal\enonic\client-test-project\src\main\resources\lib\observe'
 @ ./lib/observe/observe.es6 3:17-44
 @ ./main.es6

FAILURE: Build failed with an exception.

When importing with const httpClient = require('/lib/xp/http-client');

It will successfully build, but the project will crash on execution with the error
Caused by: com.enonic.xp.resource.ResourceNotFoundException: Resource [com.example.myproject:/lib/xp/http-client] was not found

It this an issue connected to either the starter or the library itself? I can’t find any direct way to attack this issue, any feedback is appreciated.

Seems like I figured it out.
The solution was to use const httpClientLib = __non_webpack_require__('/lib/http-client'); when importing. Same goes for any non /xp/ libs from market I’m guessing

__non_webpack_require__ ? are you are using a traspiler on the server side code?
We have allready an iplementation of require here that resolves what code to include during execution.
So your solution seems seems like the proper way to avoid the inclusion of web-pack (node?) http-client and use the enonic library http-client the correct way.

Yes, I’m using the Webpack starter. And I found this __non_webpack_require__ from an example within the same project :slight_smile:

1 Like

I typically use webpack externals.

Have not looked into whether thats easy to do with Webpack Starter.