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

LibsqlError: URL_INVALID when deploying to Render #1121

Open
rakis10 opened this issue Mar 3, 2024 · 9 comments
Open

LibsqlError: URL_INVALID when deploying to Render #1121

rakis10 opened this issue Mar 3, 2024 · 9 comments

Comments

@rakis10
Copy link

rakis10 commented Mar 3, 2024

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. (/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

@notskamr
Copy link

Facing the same issue with Cloudflare workers

@niklauslee
Copy link

niklauslee commented Mar 27, 2024

Same issue with Cloudflare pages (works well in local)

Error: Failed to publish your Function. Got error: Uncaught Error: LibsqlError: URL_INVALID: The URL is not in a valid format

Using Astro, Drizzle and Turso:

// db.ts
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client/web";

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

export const db = drizzle(client);

Environment variables in Cloudflare:

TURSO_URL=libsql://xxxxxxxxxxxx.turso.io
TURSO_AUTH_TOKEN=...

@niklauslee
Copy link

niklauslee commented Mar 27, 2024

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: withastro/astro#6130

@RafaelRamosR
Copy link

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,
});

@AirBorne04
Copy link

Hi @niklauslee the issue here is that the environment vars are not replaced at compile time. They are instead proxied to the request env vars at runtime (in cloudflare env), therefore you need to access the vars after a request has been made to your worker. This means that the suggested change from @RafaelRamosR works when called from inside an api request or inside an astro file, or middleware.

@ArshErgon
Copy link

@RafaelRamosR @AirBorne04 I am getting this error: Error [LibsqlError]: URL_INVALID: The URL is not in a valid format on vercel build failed, local development is working fine but deployment is failing

import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';
import * as schema from './schema';

export const client = createClient({
	url: import.meta.env.TURSO_URL!,
	authToken: import.meta.env.TURSO_AUTH_TOKEN
});

export const db = drizzle(client, { schema });```

@AirBorne04
Copy link

@ArshErgon you need to use a factory function createClient = () => because the env vars can only be read when a request is ongoing not statically.
Just as the example from @RafaelRamosR above.

@ArshErgon
Copy link

ArshErgon commented Apr 15, 2024

Hey @AirBorne04 thanks for the reply but error is still the same
db.ts

import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';
import * as schema from './schema';

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

export const db = drizzle(client(), { schema }); 

or can this file cause error?
drizzle.config.ts

import { Config } from 'drizzle-kit';
import 'dotenv/config';

export default {
    schema: './src/lib/db/schema.ts',
    out: './migrations',
    driver: "turso",
    dbCredentials: {
        url: process.env.TURSO_URL!,
        authToken: process.env.TURSO_AUTH_TOKEN
    }
} satisfies Config;

edit: I finally found out what I was doing wrong:
https://stackoverflow.com/questions/78328688/sveltekit-deployment-on-vercel-failed-because-of-error-libsqlerror-url-invali/78329787#78329787

@AirBorne04
Copy link

@ArshErgon yeah ok, good you got it working. That is correct in a standard VITE project you need to prefix the vars. I thought you would also be in the AstroJS / Cloudflare environment (as @niklauslee ) where the case is different. So i think also the initial report is rather related to wrong env reading than turso? @rakis10 ?

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

6 participants