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

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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