contentful / contentful.js

JavaScript library for Contentful's Delivery API (node & browser)
https://contentful.github.io/contentful.js
MIT License
1.18k stars 197 forks source link

Contentful lib return undefined deploying a NextJs app with Vercel in Node.js Runtime function environment #2302

Open OttlikG opened 1 month ago

OttlikG commented 1 month ago

Expected Behavior

getEntries load the content

Actual Behavior

In NextJs the contentful library is not loading when it's used in the API route.ts file in production. In development it all working as expected, but when deployed by Vercel as a function the library is undefined.

import contentful from 'contentful';

Here contentful is undefined.

nextjs_blur

And Sentry is giving back the following error:

TypeError: Cannot read properties of undefined (reading 'getEntries')

Steps to Reproduce

  1. Init next js app as App router.
  2. Fetch contentful's getEntries from /app/api/contentful/route.ts
  3. The library is not initialising correctly in node 20.x environment.

Environment

NextJs version: next@14.2.5 Contentful version: contentful@10.13.1 I'm using the REST api of Delivery.

cotrimelias commented 1 month ago

This doesn't seem to be Vercel specific, getting the same error on local. To solve, change to: import * as contentful from 'contentful';

OttlikG commented 3 weeks ago

Yeah that seems to be solved it. Thanks