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

165
Vistas
TypeError: coolArr.printArray is not a function when importing npm package

I created a package with an index.js that looks like this:

module.exports = class CoolArray extends Array {
  printArray() {
    console.log(this);
  }
}

I then use babel to transpile the code using es2015 syntax.

After linking my package to my test folder I can import and use my class just fine like this:

import CoolArray from 'package-name';

const coolArr = new CoolArray(1, 2, 3);

However, I can't use any of the classes functions on the coolArr object.

coolArr.printArray() gives me the error TypeError: coolArr.printArray is not a function. What am I doing wrong?


Ok I've found a solution. My index.js file now looks like this:

module.exports = class AsyncArray extends Array {
  constructor(...elements) {
    super(...elements);

    this.printArray = () => {
      console.log(this);
    }
  }
}

This link should follow you to the full answer.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This should work:

class CoolArray extends Array {
  printArray() {
    console.log(this);
  }
}

module.exports = {
    CoolArray,
};

Then it can be used like this:

import { CoolArray } from 'package-name';

const coolArr = new CoolArray(1, 2, 3);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Refactoring my index.js file to look like this allows me to use the class functions without any error:

module.exports = class AsyncArray extends Array {
  constructor(...elements) {
    super(...elements);

    this.printArray = () => {
      console.log(this);
    }
  }
}
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