Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import and use a package installed via NPM

I installed 'daterangepicker' using following commands:

npm install daterangepicker
npm install @types/daterangepicker

and then tried to import it and use like that:

import { DateRangePicker } from 'daterangepicker';

const picker = new DateRangePicker('#date-picker', {
  startDate: '01/01/2022',
  endDate: '01/31/2022'
});

but it gives me the following error:

'DateRangePicker' only reffers to a type, but is being used as a value here

in their documentation they are using jQuery to call it but I wonder if I can import it and use in .ts file somehow? http://www.daterangepicker.com/

like image 903
dchoruzy Avatar asked Nov 29 '25 22:11

dchoruzy


1 Answers

This is an old javascript module that isn't configured / setup for ES module import.

You can use this syntax to import and use it:

import * as DateRangePickerImport from 'daterangepicker';
var DateRangePicker = (DateRangePickerImport as any).default;

const picker = new DateRangePicker(document.getElementById(''), {
  startDate: '01/01/2022',
  endDate: '01/31/2022',
});

Or you can do as suggested in the comments and use require by importing @types/node and then:

var DateRangePicker = require('daterangepicker');

const picker = new DateRangePicker(document.getElementById(''), {
  startDate: '01/01/2022',
  endDate: '01/31/2022',
});

Or you can set "esModuleInterop": true in your tsconfig and then:

import DateRangePicker from 'daterangepicker';

const picker = new DateRangePicker(document.getElementById(''), {
  startDate: '01/01/2022',
  endDate: '01/31/2022',
});

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

like image 117
Zze Avatar answered Dec 02 '25 11:12

Zze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!