Error when adding gatsby-source-sanity plugin due to existing gatsby-node.ts file.
This error is happening because Gatsby is trying to import the TypeScript source file directly from gatsby-source-sanity, but the published npm package only includes the compiled JavaScript files, not the TypeScript source. When you use TypeScript in your Gatsby config files (gatsby-node.ts, gatsby-config.ts), something in your setup is causing it to look for .ts files in node_modules/gatsby-source-sanity/src/ instead of using the compiled .js files that are actually published.
Here are a few things to try:
1. Check your tsconfig.json
Make sure your tsconfig.json doesn't have settings that would cause module resolution to prefer .ts files. Your moduleResolution should typically be "node" or "node16":
{
"compilerOptions": {
"moduleResolution": "node",
// ... other settings
}
}2. Clear caches and reinstall Sometimes Gatsby's cache can cause issues when switching to TypeScript:
rm -rf .cache node_modules
npm install
# or yarn install3. Check your gatsby-config
If you're importing gatsby-source-sanity explicitly anywhere (rather than just listing it in the plugins array), make sure you're not adding a .ts extension:
// gatsby-config.ts
module.exports = {
plugins: [
{
resolve: 'gatsby-source-sanity',
options: {
// your config
}
}
]
}4. Verify package installation
Check that gatsby-source-sanity is properly installed and contains gatsby-node.js (not .ts):
ls node_modules/gatsby-source-sanity/You should see compiled .js files, not .ts files in the package.
The key thing to understand is that while your Gatsby project files can be TypeScript, the plugins you install from npm are already compiled to JavaScript, and that's what should be loaded at runtime. The error suggests Gatsby's module resolution is somehow looking for source TypeScript files that don't exist in the published package.
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.