How to import date-fns module via Aurelia CLI

I have an Aurelia CLI project using TypeScript and I want to use date-fns npm package like below.

import { addHours, subDays } from 'date-fns';

but I got error messages when I build code.

$ au build
.
.
.
 ------- File not found or not accessible ------
| Location: /Users/pampers/dev/src/github.com/user/repo/add_days/index.js
| Requested by: /Users/pampers/dev/src/github.com/user/repo/src/feature/components/creation.js
| Is this a package? Make sure that it is configured in aurelia.json and that it is not a Node.js package
 -----------------------------------------------
 ------- File not found or not accessible ------
| Location: /Users/pampers/dev/src/github.com/user/repo/add_hours/index.js
| Requested by: /Users/pampers/dev/src/github.com/user/repo/src/feature/components/creation.js
| Is this a package? Make sure that it is configured in aurelia.json and that it is not a Node.js package
 -----------------------------------------------
 ------- File not found or not accessible ------
| Location: /Users/pampers/dev/src/github.com/user/repo/add_iso_years/index.js
| Requested by: /Users/pampers/dev/src/github.com/user/repo/src/feature/components/creation.js
| Is this a package? Make sure that it is configured in aurelia.json and that it is not a Node.js package
 -----------------------------------------------
 ------- File not found or not accessible ------
| Location: /Users/pampers/dev/src/github.com/user/repo/add_milliseconds/index.js
| Requested by: /Users/pampers/dev/src/github.com/user/repo/src/feature/components/creation.js
| Is this a package? Make sure that it is configured in aurelia.json and that it is not a Node.js package
 -----------------------------------------------
 ------- File not found or not accessible ------
| Location: /Users/pampers/dev/src/github.com/user/repo/add_minutes/index.js
| Requested by: /Users/pampers/dev/src/github.com/user/repo/src/feature/components/creation.js
| Is this a package? Make sure that it is configured in aurelia.json and that it is not a Node.js package
 -----------------------------------------------
.
.
.
(more import errors)

Is there any workaround?
My aurelia.json is here.

        "dependencies": [
          {
            "name": "date-fns",
            "path": "../node_modules/date-fns",
            "main": "index"
          },

Thanks.

Can you try like this?

"dependencies": [
          {
            "name": "date-fns",
            "path": "../node_modules/date-fns/dist",
            "main": "date_fns"
          }
1 Like

Hi, @ViktorDimitrievski.
Thank you for your prompt reply.
I tried it, but date-fns package has no dist directory.
The structure of directories is like below.

node_modules/date-fns/
β”œβ”€β”€ CHANGELOG.md
β”œβ”€β”€ LICENSE.md
β”œβ”€β”€ README.md
β”œβ”€β”€ index.js
β”œβ”€β”€ package.json
β”œβ”€β”€ add_days
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ index.js.flow
β”‚   └── package.json
β”œβ”€β”€ add_hours
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ index.js.flow
β”‚   └── package.json
    .
    .
    .
β”œβ”€β”€ last_day_of_year
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ index.js.flow
β”‚   └── package.json
β”œβ”€β”€ locale
β”‚   β”œβ”€β”€ _lib
β”‚   β”‚   β”œβ”€β”€ build_formatting_tokens_reg_exp
β”‚   β”‚   └── package.json
β”‚   β”œβ”€β”€ package.json
β”‚   └── zh_tw
β”‚       β”œβ”€β”€ build_distance_in_words_locale
β”‚       β”œβ”€β”€ build_format_locale
β”‚       β”œβ”€β”€ index.js
β”‚       └── package.json
β”œβ”€β”€ max
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ index.js.flow
β”‚   └── package.json
β”œβ”€β”€ sub_years
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ index.js.flow
β”‚   └── package.json
└── typings.d.ts

259 directories, 539 files

I’ve looked over the following official docs, but I couldn’t find any solutions…
http://aurelia.io/docs/build-systems/aurelia-cli#adding-client-libraries-to-your-project

Ho about the section A Very Stubborn Legacy Library in the same chapter?

1 Like

@panchan9 the date-fns is currently not supported by cli’s default requirejs setup.
I could not figure out how to config date-fns to work. I worked on cli tracing code before, so you can take the answer as (almost) negative. But @JeroenVinke might help.

Anyway there is a way out. If you use cli + webpack, it definitely works. (au new proj, choose 3.Custom -> 3.Webpack).

1 Like

Which version of date-fns you’re using?
Because I see in the git repo of date-fns there is dist folder.

1 Like

Oh… That’s strange.
The version of date-fns is 1.29.0.

$ npm list -l date-fns

└── date-fns@1.29.0
    Modern JavaScript date utility library
    git+https://github.com/date-fns/date-fns.git
    https://github.com/date-fns/date-fns#readme

Would you mind installing the package locally with the following command?
npm i date-fns

Thank you for your valueable information!
I’m using requirejs as module loader, so… I hope that CLI supports libraries like date-fns.
One more thing. Could you tell me if a pattern of cli + systemjs works well?
Thanks.

Unfortunately, cli only uses systemjs’s AMD support. It means cli only uses systemjs as a requirejs implementation, it still uses exactly same tracing code to bundle all the js files.

I just ran through a setup with cli + systemjs + date-fns, I can confirm the behaviour is same as cli + requirejs.

This left you no choice but to use cli + webpack, unless you consider to switch to momentjs which is more popular than date-fns.

I am using momentjs and I have no complain. Anyway, I cannot judge date-fns since I have zero exp on it.

I have sent some details on gitter to cli maintainer @JeroenVinke for review. But he is a busy man, might need some days to respond.

1 Like

Oh relly???
I don’t know that we all need to know everything, and also that we can’t make mistakes. With your reaction, you just discourage people to write posts on discourse.

Sorry for off topic.

Sorry to let you feel like that. I apologize again, I should contact you privately.
Thx discourse for function to delete.

Thanks for a such detailed explanation.
Okay, then I’ll use momentjs, because I just wanted to try using date-fns with Aurelia.
And thank you very much for sharing this issue with @JeroenVinke!

Me too, I tried and failed, but at least I know there was a good reason.

My new project uses webpack so it should work there

1 Like

I’ve just gotten it to work in a non-CLI project by installing and bundling it with JSPM.