thorwebdev
thorwebdev•8mo ago

How do I run an npm script with multiple dependencies, e.g. `drizzle-kit generate:pg`?

I thought about wrapping it into a task "generate": "deno run -A npm:drizzle-kit generate:pg" in the deno.json file, but I'm getting
Task generate deno run -A npm:drizzle-kit generate:pg
error: Uncaught Error: Cannot find module 'drizzle-orm/pg-core'
Task generate deno run -A npm:drizzle-kit generate:pg
error: Uncaught Error: Cannot find module 'drizzle-orm/pg-core'
in that case
3 Replies
Deno AI Helper
Deno AI Helper•8mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the message you provided, it seems like there might be an issue with how you're trying to run the npm script within Deno. In Deno, there isn't a direct equivalent to npm scripts as you would have in a Node.js environment. Deno tasks in the deno.json file are used to run Deno-specific commands. If drizzle-kit is an npm package and you're trying to use it within Deno, you would typically need to use a compatibility layer or a tool that allows you to import npm packages in Deno. However, the error message you're seeing suggests that the module 'drizzle-orm/pg-core' cannot be found. This could be due to a variety of reasons, such as the module not being installed correctly, or the module not being compatible with Deno. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of the advice given. If you continue to experience issues, you may want to consider reaching out to the maintainers of drizzle-kit or looking for community support in forums or chat groups that focus on Deno and the specific packages you are using.
Kevin Whinnery
Kevin Whinnery•8mo ago
It looks like we have a few issues at play here. The first is that drizzle-kit expects that drizzle-orm has already been installed to node_modules in the current folder (it doesn't work if you run npx drizzle-kit generate:pg without doing this either). My recommendation here is that (for now) you manage these dependencies with an npm client.
npm i drizzle-orm postgres
npm i -D drizzle-kit
npm i drizzle-orm postgres
npm i -D drizzle-kit
Once you do this, however, running
deno run -A npm:drizzle-kit generate:pg
deno run -A npm:drizzle-kit generate:pg
Still produces an error. This is an npm compatibility error that has been reported here: https://github.com/denoland/deno/issues/19826 - it would appear that we don't have the right shims in place for the worker thread API for core Node. To run the migration generation, you'd need to run this through Node today as well 😦
npx drizzle-kit generate:pg
npx drizzle-kit generate:pg
However, the actual client code works just fine in Deno:
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { users } from "./schema.ts";

const connectionString = Deno.env.get("DATABASE_URL") || "";

const client = postgres(connectionString);
const db = drizzle(client);

const allUsers = await db.select().from(users);
console.log(allUsers);
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { users } from "./schema.ts";

const connectionString = Deno.env.get("DATABASE_URL") || "";

const client = postgres(connectionString);
const db = drizzle(client);

const allUsers = await db.select().from(users);
console.log(allUsers);
Kevin Whinnery
Kevin Whinnery•8mo ago
Gist
Drizzle on Deno Workaround
Drizzle on Deno Workaround. GitHub Gist: instantly share code, notes, and snippets.