bloomreach / spa-sdk

Apache License 2.0
17 stars 14 forks source link

Nextjs example with relevance broken in 18.0.1 #7

Closed sergiomensing closed 1 year ago

sergiomensing commented 1 year ago

We are trying to upgrade the SDK in our frontend project (next) to 18.0.1 but there seems to be an error where Next expects another JS format then ESM. But for relevance the only file in the package code is the ESM version.

So when we try to import the relevance function, this results in the following error:

error - Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /path/to/project/node_modules/@bloomreach/spa-sdk/package.json
    at new NodeError (node:internal/errors:393:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:358:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:612:7)
    at resolveExports (node:internal/modules/cjs/loader:529:36)
    at Module._findPath (node:internal/modules/cjs/loader:569:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:981:27)
    at mod._resolveFilename (/path/to/project/node_modules/next/dist/build/webpack/require-hook.js:27:32)
    at mod._resolveFilename (/path/to/project/node_modules/next/dist/build/webpack/require-hook.js:27:32)
    at mod._resolveFilename (/path/to/project/node_modules/next/dist/build/webpack/require-hook.js:27:32)
    at Module._load (node:internal/modules/cjs/loader:841:27) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED',
  page: '/[[...route]]'
}

When trying the provided nextjs example by this Bloomreach SDK it also produces this error.

beetlerom commented 1 year ago

@sergiomensing Thank you for reporting this issue. For testing reasons, which nextjs version are you using (I understand this is reproducible with the example version, but we could test with both)?

sergiomensing commented 1 year ago

Hi @beetlerom, we are currently using version 13.0.6 of nextjs.

drq commented 1 year ago

we are seeing the same issue with 13.1.1

We are able to work around by using following next.config.js `/* @type {import('next').NextConfig} / const path = require('path')

const nextConfig = { reactStrictMode: true, swcMinify: false, compiler: { removeConsole: false, styledComponents: true, }, transpilePackages: ['@bloomreach/spa-sdk'], }

module.exports = nextConfig`

We have also upgraded node to 16.16.0 and npm to 8.11.0

beetlerom commented 1 year ago

It seems like updating to node 16 fixes the issue, but it's not clear why, we will investigate further.

joerideg commented 1 year ago

We would have to check the build output for the relevance module. I'm guessing its only in esm and not cjs and therefore next.js fails compiling it on the server side (although its fine for the main app I guess?). Will have to look into it further.

sergiomensing commented 1 year ago

FYI, we are using node 18

sergiomensing commented 1 year ago

@joerideg, do you maybe have an update for us on this issue?

joerideg commented 1 year ago

Created https://issues.onehippo.com/browse/SPASDK-163 for progress tracking

joerideg commented 1 year ago

Hi @sergiomensing we're gonna look into it, its likely just a matter of tweaking the build output such that it also outputs a commonjs bundle

joerideg commented 1 year ago

Hi @sergiomensing , We just released 21.0.0, adding a CJS bundle of the express module. This should resolve your problem! I've tried it with a newly generated Next.js 13 app and it seems to build (and render) just fine now 👍🏻