Here is my simplified test that works with netlify dev locally:
File: players.js
import "dotenv/config";
import { connect } from "@planetscale/database";
const DBConfig = {
host: process.env.PLANETSCALE_HOST,
username: process.env.PLANETSCALE_USERNAME,
password: process.env.PLANETSCALE_PASSWORD,
};
const conn = connect(DBConfig);
export const handler = async () => {
const { rows } = await conn.execute("SELECT * FROM players");
console.log([...rows].map((player) => player.last_name));
return { statusCode: 200 };
};
And the output:
Request from ::1: GET /.netlify/functions/players
[
'Thomas', 'Stewart', 'Wilson',
'Howard', 'Parker', 'Howard',
'Delle Donne', 'Austin', 'Jones',
'Smith', 'Griner', 'Gray',
'Taurasi', 'Sabally', 'Magbegor',
'Vandersloot', 'Smith', 'Williams',
'Collier', 'Gray', 'Shepard',
'Ogwumike', 'Boston', 'Wheeler',
'Juhász', 'Brown', 'Onyenwere',
'Horston', 'Russell', 'Jones',
'Gustafson', 'Cloud', 'Sykes',
'Hawkins', 'Hamby', 'McCowan',
'Stevens', 'Atkins', 'Fankam Mendjiadeu',
'Ogunbowale', 'Coffey', 'Bonner',
'Ionescu', 'Canada', 'Whitcomb',
'Nelson-Ododa'
]
Response with status 200 in 233 ms.
When pushing to netlify and trying to run it, here are the errors I receive:
An unhandled error in the function code triggered the following message:
Runtime.ImportModuleError - Error: Cannot find module '/var/task/node_modules/@planetscale/database/dist/cjs/index.js'
Stack trace
Runtime.ImportModuleError: Error: Cannot find module '/var/task/node_modules/@planetscale/database/dist/cjs/index.js'
at _loadUserApp (file:///var/runtime/index.mjs:997:17)
at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:21)
at async start (file:///var/runtime/index.mjs:1192:23)
at async file:///var/runtime/index.mjs:1198:1
Connection details
Netlify internal ID: 01H7JFEES5S89FC919Y7DS9PKW