Next.js Discord

Discord Forum

Module Not Found - Sanity

Unanswered
Aman Singh Bhogal posted this in #help-forum
Open in Discord
Hi there, thought I'd post this here since I posted it in the Slack workspace and didn't get a response. I keep encountering the error "Module not found: Can't resolve ".../.../sanity/client" when I deploy my project to Vercel.

Steps taken to try and remedy it:
- Change to absolute paths (from import aliases)
- Double checked client file exists in dir
- Ensured sanity-client is installed in both root and sanity dir package.json files
- Ensured env variables are configured in project settings
- Added sanity dir to .vercelignore (since this causes other errors)
- Redeployed (without build cache) incrementally after doing these steps

Most of these steps have been followed from advice by others after looking online.

Steps to reproduce error:

- Git push (auto-deploy to Vercel.)

Could someone take a quick look and suggest a way to fix it? It might be something blaringly obvious that I've overlooked. Many thanks.

https://github.com/asbhogal/Audiophile-E-Commerce-Website

31 Replies

Plott Hound
Is it working in local dev?
Did you try a local build?
Yeah its working perfectly fine in local dev on port 3000
Trying the local build now
Plott Hound
thanks, let me know if you get any errors
Hi there - just ran a build and npm start and don't encounter the same issue. Seems only to happen when I deploy. I found these 404 errors in the console.log though. Not sure if they're related or isolated incidences.
Plott Hound
can you try running the build locally to check it works?
sorry for the hoops i just want to be thorough since this is an odd issue
Yeah no problem. I ran the build locally and it seems fine. (npm run build, then npm run start)
Plott Hound
ok it might be something to do with the build cache on vercel. can you try redeploying a fresh instance on vercel?
Sure. Just redeployed the most recent failed deployment without using the existing build cache and had the same error
Plott Hound
can you try a completely new deployment on vercel?
because this issue is really odd.
considering it is building and running fine locally your paths must be correct
something might have got messed up on vercels side
No problem - what's the best way to create a new deployment? Everything is synced in terms of Git, so I can do it through the CLI, or just delete and create the project again
Plott Hound
maybe delete the project and do it again
if you can do that
Sure
Plott Hound
i usually import from git but the cli should work too
Just imported the repo via the Dashboard and the issue is showing again
Plott Hound
strange, let me download your repo and do some testing
No probs, thanks for your time
Plott Hound
thank me if i fix it haha
ok i downloaded it and went to run the build
 ✓ Compiled successfully
   Linting and checking validity of types  ...Failed to compile.

./sanity/sanity.config.ts:3:26
Type error: Cannot find module '@sanity/vision' or its corresponding type declarations.

  1 | import {defineConfig} from 'sanity'
  2 | import {structureTool} from 'sanity/structure'
> 3 | import {visionTool} from '@sanity/vision'
    |                          ^
  4 | import {schemaTypes} from './schemas'
  5 |
  6 | export default defineConfig({
npm i @sanity/vision got rid of that
now im hitting this error which i assume is because i dont have your api credentials
â–² Next.js 14.1.0

   Creating an optimized production build ...
 ✓ Compiled successfully
 ✓ Linting and checking validity of types    
   Collecting page data  ..Using the Sanity client without specifying an API version is deprecated. See https://www.sanity.io/help/js-client-api-version
Error: Configuration must contain `projectId`
i need to get back to work but i'll have another look this evening if nobody esle has solved it
Okay sure no problem, thanks 🙏
@Plott Hound strange, let me download your repo and do some testing
So I tried the whole cache removal and re-deployment again, switched to import aliases and still no luck. In the end I had to just insert the client.ts content directly in the Categories.tsx. It's not ideal and, if it ends up being in other async components, is verbose, but it's the only way I can get it working at the moment. I was hoping it would just fix itself, since I had another problem with a missing aws4 module w/ MongoDB for a different repo which just sorted itself out with Vercel. Anyway, it works 😂