Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Failed to resolve module "mysql2" #760

Open
yjavaherian opened this issue Nov 23, 2024 · 4 comments
Open

[Bug]: Failed to resolve module "mysql2" #760

yjavaherian opened this issue Nov 23, 2024 · 4 comments

Comments

@yjavaherian
Copy link

Problem description

Hi. im trying to deploy my first fresh project with deno deploy. all goes well until the final step of "Upload to DenoDeploy" in my github action which i get the following error:

Error: The deployment failed: BOOT_FAILURE

Failed to resolve module "mysql2" from "file:///node_modules/.deno/[email protected]/node_modules/drizzle-orm/mysql2/driver.js"

off course i have this module in my deno.json file and i even install it using npm in the install section just to be sure.
i have no problem running my code locally and so I do not understand where the problem is.

Steps to reproduce

try to deploy using the following github action:

name: Deploy
on:
  push:
    branches: main
  pull_request:
    branches: main

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest

    permissions:
      id-token: write # Needed for auth with Deno Deploy
      contents: read # Needed to clone the repository

    steps:
      - name: Clone repository
        uses: actions/checkout@v4

      - name: Install Deno
        uses: denoland/setup-deno@v2
        with:
          deno-version: v2.x

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: lts/*

      - name: Install step
        run: "npm install mysql2"

      - name: Build step
        env: 
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
          DOWNLOAD_URL: ${{ secrets.DOWNLOAD_URL }}
        run: "deno task build"

      - name: Upload to Deno Deploy
        uses: denoland/deployctl@v1
        with:
          project: "libgen"
          entrypoint: "main.ts"
          root: "."

Expected behavior

to be able to successfully deploy my webapp

Environment

  • Deno version: 2.1.1

Possible solution

No response

Additional context

No response

@magurotuna
Copy link
Member

Would it be possible to create a minimal code snippet that can reproduce your issue? Specifically, what would be useful here is how your code imports and uses npm:[email protected].

@yjavaherian
Copy link
Author

@magurotuna

Would it be possible to create a minimal code snippet that can reproduce your issue? Specifically, what would be useful here is how your code imports and uses npm:[email protected].

absolutely!
here is a minimal reproduction of the issue:
https://github.com/yjavaherian/drizzle-deno-deploy-demo.git

@yjavaherian
Copy link
Author

@magurotuna

I have found a possible workaround as a temporary fix until further investigation is made on this.
I simply manually imported mysql in my main file and the issue was resolved.

I was trying to create a database connection as following:

import { drizzle } from "drizzle-orm/mysql2";
const db = drizzle('DATABASE_URL');

now, I simply manually add mysql:

import mysql from "mysql2/promise";

import { drizzle } from "drizzle-orm/mysql2";
const db = drizzle('DATABASE_URL');

@magurotuna
Copy link
Member

magurotuna commented Nov 26, 2024

@yjavaherian Thank you so much for creating a repo with minimal example! That is super useful.
Now I've figured out where the cause is. drizzle-orm specifies mysql2 dependency in its peerDependencies section, meaning that library users need to install the required version of mysql2. And you did it properly in your repo by using nodeModulesDir: auto and declaring mysql2 in imports section.
However, in Deno Deploy, local node_modules is not used and the dependency resolution happens all in the system during deploy. At this point, the system interprets like the following:

  • npm:drizzle-orm is used in the code, so this needs to be included in the deployment
  • Then tries to resolve the dependencies of npm:drizzle-orm. npm:mysql2 is NOT in dependencies section in its package.json, so it's NOT included in the deployment

As a result, you ended up getting the Failed to resolve module "mysql2" from "file:///node_modules/.deno/[email protected]/node_modules/drizzle-orm/mysql2/driver.js" error at runtime.

This is explaining why your workaround solves the issue - by explicitly doing import mysql from "mysql2/promise"; in your code, the build system decides to include npm:mysql2 in the deployment.
So just writing something like below in your entrypoint file should also work:

import "mysql2";

I think the system may need to respect nodeModulesDir: auto in order to fix this issue. I'll put it in our issue tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants