Search code examples
angulartypescriptmomentjsluxon

How to use Luxon with Angular2?


I was looking for a library to use with Angular2 to handle dates and make diff/add, translations, etc ... I tried to install Luxon (http://moment.github.io/luxon/) by installing it via npm install --save-dev @types/luxon but it fails.

I always have errors like below when I compile my code:

Module not found: Error: Can't resolve 'luxon' in 'D:\App\src\app\planning-team' resolve 'luxon' in 'D:\App\src\app\planning-team' Parsed request is a module using description file: D:\App\package.json (relative path: ./src/app/planning-team) Field 'browser' doesn't contain a valid alias configuration

Does anyone already use Luxon with Angular ? How do you do ?

Thanks for your help. Regards, Mike


Solution

  • This worked for me:

    npm i luxon
    
    npm i @types/luxon -D
    

    Then your import:

    import { DateTime } from 'luxon';
    

    Now you can call const now = DateTime.local() in your Typescript.