My gatsby-node.js file is becoming huge, so I am abstracting it out in different files. However, I would like to use Typescript for the helper files.
I tried 2 things
helper.js) and called it from gatsby-node.js file. It worked fine.helper.ts, but I see failures in gatsby develop commandError: Cannot find module './src/generators/usda/helper.ts'
Require stack:
- /Users/hhimanshu/code/prsnl/ts/forkfacts/gatsby-node.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/bootstrap/resolve-module-exports.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/bootstrap/load-plugins/validate.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/bootstrap/load-plugins/load.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/bootstrap/load-plugins/index.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/bootstrap/load-config-and-plugins.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/services/initialize.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/services/index.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/state-machines/develop/services.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/state-machines/develop/index.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/node_modules/gatsby/dist/commands/develop-process.js
- /Users/hhimanshu/code/prsnl/ts/forkfacts/.cache/tmp-18033-PnVyC3Y4PhuY
- loader.js:889 Function.Module._resolveFilename
internal/modules/cjs/loader.js:889:15
- loader.js:745 Function.Module._load
internal/modules/cjs/loader.js:745:27
- loader.js:961 Module.require
internal/modules/cjs/loader.js:961:19
- v8-compile-cache.js:159 require
[forkfacts]/[v8-compile-cache]/v8-compile-cache.js:159:20
- gatsby-node.js:7 Object.<anonymous>
/Users/hhimanshu/code/prsnl/ts/forkfacts/gatsby-node.js:7:55
- v8-compile-cache.js:192 Module._compile
[forkfacts]/[v8-compile-cache]/v8-compile-cache.js:192:30
- loader.js:1101 Object.Module._extensions..js
internal/modules/cjs/loader.js:1101:10
- loader.js:937 Module.load
internal/modules/cjs/loader.js:937:32
- loader.js:778 Function.Module._load
internal/modules/cjs/loader.js:778:12
- loader.js:961 Module.require
internal/modules/cjs/loader.js:961:19
- v8-compile-cache.js:159 require
[forkfacts]/[v8-compile-cache]/v8-compile-cache.js:159:20
- resolve-module-exports.ts:197 resolveModuleExports
[forkfacts]/[gatsby]/src/bootstrap/resolve-module-exports.ts:197:26
- validate.ts:349 forEach
[forkfacts]/[gatsby]/src/bootstrap/load-plugins/validate.ts:349:31
Is it possible to leverage TypeScript to write functions and call them from gatsby-node.js?
Thanks
Short answer - no, Long answer, yes - if you have the correct setup. Typescript is a superset of javascript, which means - typescript needs to be "transpiled" into javascript first before it can be executed by any javascript engine (there is no such thing as a "typescript engine")
There are many projects out there to achieve this like babel, SWC or ESBuild. But as you already mentioned gatsby, just have a look into a guide like this:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-gatsby-project-with-typescript
Or just google "transpile typescript gatsby.js" or something like this :)