contentful.js: Contentful.js doesn't work with Nuxt3
Expected Behavior
Contentful.js SDK works with Nuxt3 when building for production.
Actual Behavior
When creating a plugin to use Contentful:
import * as contentful from "contentful";
It works fine locally when running npm run dev.
However, when building for production, this happens:
[nuxt] [request error] contentful.createClient is not a function
Steps to Reproduce
-
Create an entry with a “title” field in Contentful.
-
Create a project using Nuxt3:
npx nuxi init nuxt-appcd nuxt-app -
Install Contentful.js
npm install contentful -D -
Create the file
plugins/contentful.jsand add:
import * as contentful from "contentful";
export default defineNuxtPlugin((nuxtApp) => {
const client = contentful.createClient({
space: "YOUR_SPACE_ID",
accessToken: "YOUR_ACCESS_TOKEN",
});
nuxtApp.provide("contentfulClient", client);
});
- Then, in
app.vue:
<template>
<div>
{{ entry.fields.title }}
</div>
</template>
<script setup>
const { $contentfulClient } = useNuxtApp();
const entry = await $contentfulClient.getEntry("YOUR_ENTRY_ID");
</script>
- Run
npm run devand see that the title appears as expected. - Run
npm run build, thennpm run preview. An error 500 occurs, in the log you see:[nuxt] [request error] contentful.createClient is not a function
Context
I’m trying to deploy my application on Netlify.
Environment
- Language Version: Node v16.14.0
- Package Manager Version: NPM 8.3.1
- Browser Version: Version 100.0.4896.127 (Official Build) (x86_64)
- Operating System: macOS Monterey v12.2.1
Darwin *** 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64 x86_64 - Package Version:
"contentful": "^9.1.25"and"nuxt": "3.0.0-rc.1" - Which API are you using?: Trying to use Contentful JavaScript Content Delivery Library
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 11
- Comments: 26 (6 by maintainers)
I had this same issue. Worked in Dev, but not in Production. The below code solved my problem. Basically, in Production you must use contentful.createClient. In Dev you can use createClient.
Hope this helps someone else.
You don’t need to switch to the beta version of contentful to fix this. The beta version seems to have some issues (it didn’t respect the
includesetting on my calls when I tried it for example). In the built server, thecreateClientfunction ends up on thecontentful.defaultobject instead of thecontentfulobject. I’m sure there’s an explanation, probably something to do with interoperability between CommonJS and ES Modules.Anyway, I solved it like this:
I’m using nuxt 3.2.3 and contentful 9.3.3.
I had to ignore this with
/* eslint-disable import/namespace */. The solution from @eriklindgren is working for me.Darwinv18.14.13.3.12.3.1vitetarget,ssr,experimental,runtimeConfig,build,tailwindcss,vite,css,image,modules,i18nThe solution still works for me on nuxt 3.3.1 and contentful 9.3.5. I build the app with
nuxi buildand then run it withnode .output/server/index.mjs. In the server/shell, thecontentful.defaultobject has thecreateClientfunction and in the client/browser thecreateClientfunction is on thecontentfulobject itself.If I run the app with
nuxi dev, thecreateClientfunction is on thecontentfulobject in both server and client 🤷Hey @marcolink just want to say thanks for the help and that it’s likely a Nuxt/Vite issue or something else on my side. I don’t think you need to look into this any more.
Thanks @fmarcheski! I had to do the same thing, ended up forgetting to post it here. That solution works for me too.
I’ve run into the same issue in an Astro project. Interestingly in my case I only have this issue when building the site, not whilst Vite is running in watch mode.
I’ve played around with the stable and beta version of this package, along with named vs default imports and found this combination to work for me in both dev and production mode:
Install the beta version with
Then switch your usage like this