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