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

151
Visualizações
Relative path not working but absolute works

I'm trying to load an image to use it with canvas in node.js. Always getting a not found error. Here's the code:

const Canvas = require('canvas')

async function execute() {

    const canvas = Canvas.createCanvas(1080, 611);
    const context = canvas.getContext('2d');

    const license = await Canvas.loadImage('../../data/media/images/license.jpg');
    context.drawImage(license, 0, 0, canvas.width, canvas.height);
}

execute();

It somehow works with the path from the explorer (P:\\bot stuff\\darling.js\\src\\data\\media\\images\\license.jpg).

The thing that confuses me even more is that const { color } = require('../../data/config.json'); works perfectly fine, and that in the same file...

Folder Structure:

Folder

Node.js Version 16.10; Windows 10 Pro 21H2

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The behavior of require(...) is always resolved according to its own rules in the relative path of the file by the require.resolve(...) function.

However, Canvas.loadImage. Because it's loading part implemented in C, it can have different behavior than Node and has no context for the current js file location.

I recommend you to resolve the path based on __dirname explicitly.

about 4 years ago · Juan Pablo Isaza Relatório
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