Search code examples
node.jsangulartypescriptionic-frameworkcapacitor

Is there an equivalent of require().default in TypeScript?


I'm trying to build a small Todo.txt app for Android, using Ionic with Angular and Capacitor.

To handle the Todo.txt file, I am using a todotxt-parse package. So far, I have this on my page.ts file, and it's working:

declare var require: any; // <- This isn't nice :(
const TodoTxt = require('todotxt-parse').default;
const todo = new TodoTxt('Review pull request +TodoTxtTouch @github`');
console.log(todo.parse()); // <- Outputs an object with task metadata

However, I understand that that declare var require: any; is not a good practice.

Importing the module like this: import { TodoTxt } from 'todotxt-parse'; causes an error TS2305: Module /node_modules/todotxt-parse/lib/index"' has no exported member 'TodoTxt'

On the other hand, changing the module's index.ts file from export default class TodoTxt { ... } to export class TodoTxt { ... } causes a TypeError: todotxt_parse__WEBPACK_IMPORTED_MODULE_3__.TodoTxt is not a constructor


Solution

  • import TodoTxt from 'todotxt-parse'
    

    https://www.typescriptlang.org/docs/handbook/modules.html