Critical dependency warning when running in Next.js
Summary
When including Graphile Worker in a Next.js project, dependency warnings are thrown.
Steps to reproduce
import {quickAddJob} from "graphile-worker";
export async function POST(req: Request) {
const payload = req.text();
const job = await quickAddJob(
{ connectionString: process.env.DATABASE_URL },
"test-task",
{payload},
);
console.log(`Job added: ${job.id}`);
return new Response(null, {status: 200});
}
Expected results
The job gets enqueued.
Actual results
The job is enqueued, but this warning is shown in the console:
[22:10:45.274] WARN (next.js/1652545): ./node_modules/graphile-worker/node_modules/cosmiconfig/dist/loaders.js
Critical dependency: the request of a dependency is an expression
Import trace for requested module:
./node_modules/graphile-worker/node_modules/cosmiconfig/dist/loaders.js
./node_modules/graphile-worker/node_modules/cosmiconfig/dist/index.js
./node_modules/graphile-worker/dist/config.js
./node_modules/graphile-worker/dist/preset.js
./node_modules/graphile-worker/dist/index.js
./src/app/api/test-worker/route.ts
prefix: "warn"
Additional context
I tried modifying my Next.js config as suggested in #345:
config.module.rules.push(
{
test: [
path.resolve("node_modules/pg/lib/native"),
path.resolve("node_modules/import-fresh"),
path.resolve("node_modules/graphile-worker/dist/getTasks"),
],
loader: "null-loader",
},
);
But that only partly solved the issue. There were more warnings displayed before changing the config, though.
Possible Solution
I suspect there is some way to exclude a certain module to make the warnings go away, but nothing I have tried has worked so far.
I think the solution for this would be to add "exports" to package.json and then export a subpath for WorkerUtils ("exports": {"./utils": {...}}) that does not need to contain any of the dynamic task loading logic. Further, we should make a stripped down WorkerUtils that doesn't know how to run migrations (since that also loads files). You'll still have issues bundling it since the pg library attempts to load pg-native, but this'll help.
Alternatively, just use the Next configuration to stub out all the problematic parts that you don't need.
Or you could just call graphile_worker.add_jobs() database function directly from your code and skip the worker utils to do so entirely.
@chrboe you ever find a solution you like for this? I'm hitting the exact same problem trying to integrate graphile-worker with typescript tasks into a typescript-based next.js app.