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

100
Visualizações
Running into reference error with lodash.template

Edit

I ended up replacing all '$' in the file with '$' and doing the following:

await fsPromises.writeFile(
    targetFilePath,
    compiledSourceFile({
        schemaName: schema.schema_name
    }).replace('$', '$', 'ig')
);

I have a simple file with the following contents:

const { executeQuery } = require('../../db');

const getAllTableRecords = async ({ tableName }) =>
    executeQuery({
        query: `
            SELECT
                *,
                '${tableName}' as "table_name"
            FROM <%= schemaName %>.${tableName}
        `,
    });

module.exports = {
    getAllTableRecords,
};

I try to programmatically read this file and output it to a different directory:

import path from 'path';
import { promises as fsPromises } from 'fs';
import { template } from 'lodash';

const sourceFilePath = path.join(__dirname, 'utils', 'index.js');
const compiledSourceFile = template(sourceFile);
const targetFilePath = path.resolve(__dirname, 'temp', 'utils', 'index.js');

const [sourceFile] = await Promise.all([
    fsPromises.readFile(sourceFilePath, 'utf-8'),
    fsPromises.mkdir(targetFilePath, { recursive: true })
]);

await fsPromises.writeFile(targetFilePath, compiledSourceFile({ schemaName: schema.schema_name }));

All this does is read the template file, replace the values in the template and recursively create and write to the target destination.

But this throw an error,

ReferenceError: tableName is not defined

It seems as though lodash.template can't process the template literal ${}. It keeps failing at this line,

SELECT
    *,
    '${tableName}' as "table_name"
    ^^^^^^^^^^^^^^

What are my options here if I want to stick to template literals?

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