Failed to resolve module specifier "date-fns".

See original GitHub issue

Hi, I received this message:

Uncaught TypeError: Failed to resolve module specifier “date-fns”. Relative references must start with either “/”, “./”, or “…/”. in my Chrome console when I tried: import {format} from 'date-fns'

image image image This is my first time installing a package instead of running on cdn, and I would love to get it to work.

Many thanks! Ken

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
maximillianfxcommented, Aug 22, 2020

Alright! @ken-1998 You can use the CDN from date-fns in your page.html, like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Teste</title>
</head>
<script src="http://cdn.date-fns.org/v1.9.0/date_fns.min.js"></script>
<script src="./sample.js"></script>
<body>
    
</body>
</html>

and in sample.js call the library with the word dateFns.

console.log(dateFns.isToday(new Date()))
var result = dateFns.format(
    new Date(2020, 7, 21),
    'MM/DD/YYYY'
)

console.log("Date: " + result)

You will use date-fns package from npm-package in projects like node-js, react, vue, angular, etc. It’s not recomended use that approach to simple html pages because the package works like a module, e.g ES6, and the browser dont read ES6, you’ll need a Babel transpiler to convert ES6 code to JS native code.

See more info here.

1reaction
ken-1998commented, Aug 22, 2020

I see! Thank you @maximillianfx ! Have a great weekend!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'date-fns/addDays' in 'C ...
You need to install the date-fns package from NPM using npm install --save date-fns .
Read more >
Cannot find module 'date-fns' error | bobbyhadz
To solve the error "Cannot find module 'date-fns'", make sure to install the date-fns package by opening your terminal in your project's root...
Read more >
failed to resolve module specifier "axios". relative references ...
I get Uncaught TypeError: Failed to resolve module specifier "axios". Relative references must start with either "/", "./", or ". Favicon for you.com ......
Read more >
Failed to resolve module specifier | by Sami C. - Medium
Failed to resolve module specifier. Today I got stuck when trying to load a module: import(`folder/myModule.js`).then((response) => ...
Read more >
date-fns-tz - npm
Time zone support for date-fns v2 with the Intl API. ... property in your project's package.json to either module , for ESM, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found