Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!