WebpackError: Configuration must contain `projectId`

2 replies
Last updated: Nov 17, 2020
Hey everyone, I’ve noticed this has come up a few times before but there doesn’t seem to be a clear solution. Currently we are using the sanity client to fetch data for our Gatsby app. This is our configuration:

const client = require("@sanity/client");

export const sanityClient = client({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_NODE_ENV,
  useCdn: false,
});
Everything ran fine during development, but we ran in some issues when trying to build. This is what we are getting:


  49 |
  50 |   if (projectBased && !newConfig.projectId) {
> 51 |     throw new Error('Configuration must contain `projectId`');
     | ^
  52 |   }
  53 |
  54 |   var isBrowser = typeof window !== 'undefined' && window.location && window.location.hostname;


  WebpackError: Configuration must contain `projectId`

  - config.js:51
    node_modules/@sanity/client/lib/config.js:51:1

  - sanityClient.js:78
    node_modules/@sanity/client/lib/sanityClient.js:78:1
Any help with this would be great appreciated. Thanks!
AI Update

This is a classic environment variable issue during the Gatsby build process. The error means that process.env.SANITY_PROJECT_ID is undefined when Gatsby runs your build, even though it works fine in development.

The Problem

Gatsby has specific requirements for environment variables. In Gatsby, environment variables need to be:

  1. Prefixed with GATSBY_ to be available in browser/build code
  2. Properly loaded from .env files

Your current setup uses process.env.SANITY_PROJECT_ID, but Gatsby isn't making this available during the build process.

The Solution

Option 1: Use Gatsby-prefixed environment variables (Recommended)

Rename your environment variables in your .env file:

GATSBY_SANITY_PROJECT_ID=your-project-id
GATSBY_SANITY_DATASET=your-dataset

Then update your client configuration:

const client = require("@sanity/client");

export const sanityClient = client({
  projectId: process.env.GATSBY_SANITY_PROJECT_ID,
  dataset: process.env.GATSBY_SANITY_DATASET,
  useCdn: false,
});

Option 2: Use dotenv with Gatsby's config

If you want to keep your variable names, you can explicitly load them in gatsby-config.js:

require("dotenv").config({
  path: `.env.${process.env.NODE_ENV}`,
});

module.exports = {
  // your gatsby config
};

Then make sure you're accessing the variables correctly and they're defined in your .env file at the root of your project.

Additional Tips

  1. Check your .env file location - It should be at the root of your Gatsby project
  2. Restart your development server after changing environment variables
  3. Don't commit .env files - Add them to .gitignore
  4. Set variables in your hosting platform - For production builds (Netlify, Vercel, etc.), you need to set these environment variables in your hosting platform's dashboard with the GATSBY_ prefix

Note: You have useCdn: false which is fine for builds, but consider setting useCdn: true for production client-side requests to get better performance through Sanity's CDN.

Hey ! This might me a problem coming from your .env not being properly loaded during build-time.
Maybe you can try to replace your
process.env.SANITY_PROJECT_ID
by a string and try to build again to see if it works.
Hi Hervé,Thank you for your reply! Strangely enough when we tried this earlier it didn’t work, but now it does!
Cheers for your help!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?