Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
NextJS with Serverless Mysql ServersideProps - fs error

I'm trying to build an app in nextjs and I've stumbled upon some issues. I'd like to do a query call inside my getServerSideProps() but I get the error:

error - ./node_modules/mysql/lib/protocol/sequences/Query.js:2:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/mysql/lib/Connection.js
./node_modules/mysql/index.js
./node_modules/serverless-mysql/index.js
./lib/db.js
./lib/components/admin/_bans.js
./pages/admin/index.js

This is easily solved if I put this code inside an api endpoint but I'd like not do so. Sometimes it's just better to do everything inside the component. But why would I get this error. I know it's because something regarding server but then again, getServersideProps(), does't that run on the server? Am I missing something obvious here?

Below is my getServerSideProps()

export async function getServerSideProps(context) {

    const results = await sql_query(`
        SELECT * from table 
    `);
    
    let result = JSON.parse(JSON.stringify(results))[0];
    
    let posts;

    return {
        props: {}, // will be passed to the page component as props
    }
}

As said, I am using the Serverless-mysql package. Here is my db.js which handles my query calls.

import mysql from 'serverless-mysql'

export const db = mysql({
  config: {
    host: process.env.MYSQLHOST,
    database: process.env.DATABASE,
    user: process.env.USERNAME,
    password: process.env.PASSWORD,
  },
})

export async function sql_query(query_string,values = []) {
  try {
    const results = await db.query(query_string, values)
    await db.end()
    return results
  } catch (e) {
    throw Error(e.message)
  }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda