Cant get date-fns to work

Hi there i am trying to play around with packages and installed the date-fns package but cant seem to use it, i have a feeling its because of how my folders are set up but dont know the right way
im not sure what i have done is correctt so i ran npm init -y on mmy mac terminal then npm install date-fns

but getting error message

Hi,

This is not to do with date-fns, rather with how Node handles ES Modules.
If you follow the instructions you see in the error message, you can resolve the problem. Add the following to package.json:

"type": "module",

Then the script will run without problems:

~  node script.js
2023-12-15T07:57:52.326Z
2026-12-15T07:57:52.326Z

Please note that you cannot run date-fns in the browser using this method (there seems to be a browser open in the background of your screenshot).

If you are trying to run it in the browser, you will either need to introduce a bundling step, or use the method demonstrated here:

i did set type to module but didnt work still?

Then you have to provide a bit more information.

What is your exact code? How did you install everything and how are you running it?

If I can reproduce the steps you have taken, then I will be able to help you get things working.

Hi so i learnt that i need to use a bundler first. I installed vite, i ran npm install after that and npm run dev,


i am now seeing some results but how would i go about showing these in the browser console this is what i have

With Vite running (npm run dev) and the code main.js as per your screenshot, visit http://localhost:5173 in your browser and open the console. You will see the output.

so i was just opening using vs code open with live server before and that was showing an error but your link worked, what is the difference here and how would i have known this ?

nvm tells me in the terminal silly me, thank you for all your help much appreciated

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.