Using APIs in Webpack Starter project

Enonic version: current
OS: WIN and macOS

I would like to use the webpack-starter for my “Backend”-part.
So I created a new project using the starter, deleted all client related stuff and ran a build successfilly.
Then I added the contextLib in build.gradle include "com.enonic.xp:lib-context:${xpVersion}"

In main.es6 I added the following line const contextLib = require('/lib/xp/context');
When I try to build the project I gett the following error:

ERROR in ./main.es6 2:17-43
Module not found: Error: Can't resolve '/lib/xp/context' in 'D:\Developement\enonic\test\src\main\resources'
resolve '/lib/xp/context' in 'D:\Developement\enonic\test\src\main\resources'
  using description file: D:\Developement\enonic\test\package.json (relative path: ./src/main/resources)
    Field 'browser' doesn't contain a valid alias configuration
    root path D:\Developement\enonic\test\src\main\resources
      using description file: D:\Developement\enonic\test\package.json (relative path: ./src/main/resources/lib/xp/context)
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          D:\Developement\enonic\test\src\main\resources\lib\xp\context doesn't exist
        .ts
          Field 'browser' doesn't contain a valid alias configuration
          D:\Developement\enonic\test\src\main\resources\lib\xp\context.ts doesn't exist
        .json
          Field 'browser' doesn't contain a valid alias configuration
          D:\Developement\enonic\test\src\main\resources\lib\xp\context.json doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          D:\Developement\enonic\test\src\main\resources\lib\xp\context.js doesn't exist
        .es
          Field 'browser' doesn't contain a valid alias configuration
          D:\Developement\enonic\test\src\main\resources\lib\xp\context.es doesn't exist
        .es6
          Field 'browser' doesn't contain a valid alias configuration
          D:\Developement\enonic\test\src\main\resources\lib\xp\context.es6 doesn't exist
        as directory
          D:\Developement\enonic\test\src\main\resources\lib\xp\context doesn't exist
    No description file found in /lib/xp or above
    no extension
      Field 'browser' doesn't contain a valid alias configuration
      /lib/xp/context doesn't exist
    .ts
      Field 'browser' doesn't contain a valid alias configuration
      /lib/xp/context.ts doesn't exist
    .json
      Field 'browser' doesn't contain a valid alias configuration
      /lib/xp/context.json doesn't exist
    .js
      Field 'browser' doesn't contain a valid alias configuration
      /lib/xp/context.js doesn't exist
    .es
      Field 'browser' doesn't contain a valid alias configuration
      /lib/xp/context.es doesn't exist
    .es6
      Field 'browser' doesn't contain a valid alias configuration
      /lib/xp/context.es6 doesn't exist
    as directory
      /lib/xp/context doesn't exist

webpack 5.88.2 compiled with 1 error in 3880 ms
ERROR: "build:server" exited with 1.

Process finished with exit code 1

What did I make wrong ?

Hmm that is strange, would expect the externals configuration to kick in.

Maybe try import contextLib from "/lib/xp/context" as webpack uses the module import in that starter.

Aslo if you simply want to keep it you can use _non_webpack_require but its easier to dive into the configuration of webapck to set it up.

Unfortunately that does not work either!
Are you able to reproduce this behaviour?

I found the error with help of Cody from Sourcegraph :slight_smile:

I have to add the following line in webpack.server.config.js
'/lib/xp/context': '/lib/xp/context' under externals

I recognized that this block is new in the current starter. I do not understand whay this worked in older starters - but now it works :slight_smile: