Error when adding gatsby-source-sanity plugin due to existing gatsby-node.ts file.

20 replies
Last updated: Jul 20, 2020
have used sanity on a few projects, but this is the first project im using it on where I have made the gatsby core files ts (gatsby-node etc) I get this error when adding
gatsby-source-sanity

ENOENT: no such file or directory, open '/Users/isaacmartin/Documents/code/tm-landing/web/node_modules/gatsby-source-sanity/src/gatsby-node.ts'
AI Update

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 install

3. 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.

sure this is something in the fact that i already have a
gatsby-node.ts
anyone seen this before?
Hi Isaac, could you try removing your
node_modules
folder and `yarn.lock`/`package-lock.json` files before running
npm install
to see if that changes anything?
[In your studio root folder]
hey peter, yeah still the same issue
worth noting as well removing the plugin from config array means the error is no longer there as expected (but we obviously can't source data then)
also manually moving the gatsby-node.ts into a src directory in
node_modules/gatsby-source-sanity
silences the error but it then doesn't source any data
Wonder if there needs to be a flag or something in the plugin if using TS to resolve correctly?
user M
?
Im happy to PR this if someone gives me some guidane 😬
Not sure why this is not getting picked up correctly. I’ll ask internally as well to see if someone better versed than me in TS knows why this could be happening πŸ™‚
Thanks Peter
I've converted gatsby core-files to ts in a project I'm working on, works like a charm. Maybe our code could be of some help to you (tho there will be norwegian comments πŸ˜›): https://github.com/navikt/dp-faktasider-frontend
Thanks
user Q
- did you ever see this error message?
nope, dont remember having any trouble with gatsby-source-sanity when converting to ts πŸ€”
Damn 😞
must be something else going on
Do you have any public/private repo you could share in this case (in DM if you prefer)?
Update - resolved. not related to ts. I had a spelling mistake in my env variable name
Might need the error message to be updated to say thats the reason its not working
Indeed, no way to know that through the error message πŸ™‚ Happy you can move ahead with TS!

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?