Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
NodeJS package outputting empty string

I'm trying to create my own NodeJS package that uses the chalk to output colored strings, but i'm getting empty string as output( when i run node test.js in terminal ) I tried searching online but couldn't find any reason for this. Does anyone have an idea what could be causing this error?

index.js file

import chalk from 'chalk';
import pkg from 'koa/lib/request';
const { charset } = pkg;
const fontColors = ['red','green','yellow','blue','magenta','cyan',];
export const colourfulLog = (string) => {
    const colorString = string.split(' ')
        .map(word => {
            const randclrindex = Math.floor(Math.random() * fontColors.length);
            const randColor = fontColors[randclrindex];
            return chalk[randColor][word];
        })
        .join(' ');
    console.log(colorString);
}

test.js file

import { colourfulLog } from './index.js';
colourfulLog(' Test the colorful log package');

package.json file

{
  "name": "colorful-log-ax",
  "version": "1.0.0",
  "description": "first package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

  "author": "AT",
  "license": "ISC",
  "dependencies": {
    "chalk": "^5.0.1"
  },
  "type": "module"
  
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There is an error in your index.js file:

    return chalk[randColor][word];

You are using square brackets to dynamically specify a method: chalk[randColor] is like saying chalk.randColor, then you have to apply it to word like this:

    return chalk[randColor](word);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda