Running TS scripts to create pre-compute resources as part of next.js build process
Unanswered
West African Crocodile posted this in #help-forum
West African CrocodileOP
I have some scripts that I would like to run as part of my build process for dynamically building resources.
In this case, it is a script to build a search index over a few thousand documents, which makes it not a good candidate for using the
Because this script depends on running typescript code that is also needed for the application, I can't really just do this with JS and avoid using typescript. Obviously, there are tools like
Ideally, it seems like I would be able to run code as part of the next.js build process so I can reuse all of the next compiler toolchain, but the only way I can think to do that currently is by writing a webpack plugin, which 1) seems like a lot of work and 2) not the right long term thing with turbopack migration.
EDIT: this is using app dir
In this case, it is a script to build a search index over a few thousand documents, which makes it not a good candidate for using the
instrumentation file or dynamically building the index on first use (it times out on vercel, for example)Because this script depends on running typescript code that is also needed for the application, I can't really just do this with JS and avoid using typescript. Obviously, there are tools like
tsx, but I have found issues with differences in ESM/commonjs modules that mean they don't work reliable. It also just feels really bloated to need a "second" toolchain. Ideally, it seems like I would be able to run code as part of the next.js build process so I can reuse all of the next compiler toolchain, but the only way I can think to do that currently is by writing a webpack plugin, which 1) seems like a lot of work and 2) not the right long term thing with turbopack migration.
EDIT: this is using app dir
9 Replies
So running during build process is easy, just override build command in package json
For the tsx esm issues do you have a log of the errors?
It’s not bloated to use another tool btw, if you’re solving a problem with the right tool
Btw if you really want to abstract this out then just build an npm package that has a cli entry point and also exports for whatever functions you want to expose to your nextjs app
Then import that package in your nextjs app
But I’m surprised tsx isn’t working
Because that’s the first thing I would reach for here
West African CrocodileOP
node:internal/modules/esm/resolve:303
return new ERR_PACKAGE_PATH_NOT_EXPORTED(
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /workspaces/vvp-knowledge-hub/node_modules/estree-walker/package.json
at exportsNotFound (node:internal/modules/esm/resolve:303:10)
at packageExportsResolve (node:internal/modules/esm/resolve:593:13)
at resolveExports (node:internal/modules/cjs/loader:591:36)
at Module._findPath (node:internal/modules/cjs/loader:668:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:1130:27)
at a._resolveFilename (/home/node/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/cjs/index.cjs:1:1729)
at Module._load (node:internal/modules/cjs/loader:985:27)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at <anonymous> (/workspaces/vvp-knowledge-hub/node_modules/@mdx-js/mdx/lib/plugin/recma-document.js:33:20) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
Node.js v20.11.1Is the output of tsx, it isn't clear to me why that would not work.
The one thing that is a bit "strange" about my project setup is that
package.json does not have type: module as that caused issues with other packages importing