I installed date-fns as per the following instruction on
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 / 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._pile (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
I installed date-fns as per the following instruction on https://www.npmjs./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/ 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._pile (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
Share
Improve this question
edited May 26, 2022 at 18:39
Arvind Kumar Avinash
asked Aug 3, 2021 at 17:34
Arvind Kumar AvinashArvind Kumar Avinash
79.8k10 gold badges92 silver badges135 bronze badges
5
- I think you are probably facing a syntax errors. You are missing semicolons at the end of each line. The lines you copied are just abstract statements that can be used in code. – Salvino D'sa Commented Aug 3, 2021 at 17:39
- @Salvino - No, I have tried that as well but anyway, unlike Java, JavaScript does not require a semicolon at the end of the statement. – Arvind Kumar Avinash Commented Aug 3, 2021 at 17:41
- Can you also tell us, what is the mand you are using to execute your code? – Salvino D'sa Commented Aug 3, 2021 at 17:54
-
I am clicking the run button in VSCode. I also tried
node main.js
on the terminal but encountered the same error. – Arvind Kumar Avinash Commented Aug 3, 2021 at 17:55 -
can you
cd
to your project's root directory and runnode main.js
in your terminal? – Salvino D'sa Commented Aug 3, 2021 at 17:57
2 Answers
Reset to default 5I 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"));
You are probably facing syntax errors, as you directly copy pasted the code from the documentation. Try importing the library as follows. It should work just fine.
import { format, formatDistance, formatRelative, subDays } from 'date-fns';
const mDate= format(new Date(2014, 1, 11), 'MM/dd/yyyy');
console.log(mDate);