libsql: LibsqlError: URL_INVALID when deploying to Render

Localy works, but when deploying on Render.

db.js:

let dotenv = require('dotenv').config()
const { createClient } = require('@libsql/client')
const { drizzle } = require('drizzle-orm/libsql')
const client = createClient({
  url: dotenv.parsed.TURSO_URL,
  authToken: dotenv.parsed.TURSO_AUTH_TOKEN,
})
module.exports = drizzle(client)

“dependencies”: { “@libsql/client”: “^0.5.2”, “cors”: “^2.8.5”, “dotenv”: “^16.4.5”, “drizzle-orm”: “^0.29.4”, “ejs”: “^3.1.9”, “events”: “^3.3.0”, “express”: “^4.18.2”, “express-session”: “^1.17.3”, “fs”: “^0.0.1-security”, “nodemon”: “^3.0.2”, “passport”: “^0.7.0”, “passport-local”: “^1.0.0” },

/opt/render/project/src/node_modules/@libsql/core/lib-cjs/uri.js:12 throw new api_js_1.LibsqlError(“The URL is not in a valid format”, “URL_INVALID”); ^ LibsqlError: URL_INVALID: The URL is not in a valid format at parseUri (/opt/render/project/src/node_modules/@libsql/core/lib-cjs/uri.js:12:15) at expandConfig (/opt/render/project/src/node_modules/@libsql/core/lib-cjs/config.js:35:39) at createClient (/opt/render/project/src/node_modules/@libsql/client/lib-cjs/node.js:28:52) at Object.<anonymous> (/opt/render/project/src/db.js:4:16) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions…js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18) { code: ‘URL_INVALID’, rawCode: undefined, [cause]: undefined } Node.js v20.11.1

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 29 (2 by maintainers)

Commits related to this issue

Most upvoted comments

So basically, everyone has the same problem, and there is still no fix for this. I guess it’s the downside of using an SQL variant that nobody really uses in production…

This problem is caused by undefined env variables, not libsql, to fix this make sure the env is added on the server and call it the right way. This solved my problem.

It worked for me.

import { createClient } from '@libsql/client/web';

const dbClient = () => createClient({
  url: import.meta.env.TURSO_URL,
  authToken: import.meta.env.TURSO_AUTH_TOKEN,
});

@Zaprovic yup, it will be visible to the client-side. I’m not expert in nextjs and I dont think theres other ways of accessing env in next, I google const url = import.meta.env.VITE_TURSO_DATABASE_URL?.trim(); use in next but its not possible I think.

Heres my current drizzle.config.ts file, its working try to copy my code in your file and lets pray it will work.

import type { Config } from 'drizzle-kit';
import * as dotenv from 'dotenv';
dotenv.config();

export default {
    dialect: 'sqlite',
    schema: './src/lib/server/database/drizzle-schemas.ts',
    out: './migrations',
    driver: "turso",
    dbCredentials: {
        url: process.env.VITE_TURSO_DATABASE_URL as string,
        authToken: process.env.VITE_TURSO_AUTH_TOKEN as string,
    }
} satisfies Config;

Edit: for nextjs the env file on vercel is name .env or .env.local? You can see when are uploading your .env file there, if thats .env then you may try to update this code of yours:

dotenv.config({
  path: ".env.local",
});

@ArshErgon I had just launch to production on Vercel, so yes, there’s where I am hosting my app. However in production the error does not happen. I copied and pasted the .env file in the Vercel environment variables and the deployment was successful. I still get the same issue in development though.

It is very weird, because the error points to src/db/main.ts but in the drizzle.config.ts I defined the environment variables the exact same way and it doesnt show any errors

import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./src/db/schema.ts",
  out: "./drizzle/migrations",
  dialect: "sqlite",
  driver: "turso",
  dbCredentials: {
    url: process.env.DATABASE_URL!,
    authToken: process.env.DATABASE_AUTH_TOKEN!,
  },
});

import { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";

// todo: fix .env issue
const client = createClient({
  url: process.env.DATABASE_URL!,
  authToken: process.env.DATABASE_AUTH_TOKEN!,
});

export const db = drizzle(client);

Here’s how the error appears in my vscode as well

image

If I console log the variables as you say, I first get undefined, but then I get the correct value, maybe that’s where the problem is

image

I am guessing the environment variables should be loaded ahead, before adding them to the libsqlClient configuration variable, but I don’t know how to do that

When I use url string directly in the client (instead of using import.meta.env.), it works. Maybe this is a cloudflare’s environment variable issue.

The related issue: https://github.com/withastro/astro/issues/6130