I installed date-fns as per the following instruction on https://www.npmjs.com/package/date-fns
JavaScript
x
2
1
npm install date-fns --save
2
After that my package.json
is updated with the following entry:
JavaScript
1
6
1
{
2
"dependencies": {
3
"date-fns": "^2.23.0"
4
}
5
}
6
Then, I wrote the following code from https://date-fns.org/ and it resulted in error:
JavaScript
1
4
1
import { format, formatDistance, formatRelative, subDays } from 'date-fns'
2
3
format(new Date(), "'Today is a' eeee")
4
Error:
JavaScript
1
13
13
1
import { format, formatDistance, formatRelative, subDays } from 'date-fns'
2
^
3
4
SyntaxError: Unexpected token {
5
at Module._compile (internal/modules/cjs/loader.js:703:23)
6
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
7
at Module.load (internal/modules/cjs/loader.js:628:32)
8
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
9
at Function.Module.runMain (internal/modules/cjs/loader.js:822:10)
10
at internal/main/run_main_module.js:17:11
11
12
[Done] exited with code=1 in 0.143 seconds
13
node -v
:
JavaScript
1
2
1
v12.2.0
2
Advertisement
Answer
I managed to run it successfully by using require
as shown below:
JavaScript
1
4
1
const fns = require('date-fns')
2
3
console.log(fns.format(new Date(), "'Today is a' eeee"))
4
Update:
I installed node v16.6.1 following the instructions in this answer and now I can run the following code successfully:
JavaScript
1
4
1
import { format } from 'date-fns';
2
3
console.log(format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSS"));
4