question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

relative time plugin throws: asynchronous error TypeError: v is not a function

See original GitHub issue

Describe the bug Trying to use relative time formatting for relative times less than a minute throws an error:

asynchronous error TypeError: v is not a function
    at Object.n.fromToBase (/Users/christophe.taylor/nurx/deploy/node_modules/dayjs/plugin/relativeTime.js:1:1086)
    at i (/Users/christophe.taylor/nurx/deploy/node_modules/dayjs/plugin/relativeTime.js:1:510)
    at M.n.from (/Users/christophe.taylor/nurx/deploy/node_modules/dayjs/plugin/relativeTime.js:1:1265)
    at Object.exports.app_status_table (/Users/christophe.taylor/nurx/deploy/lib/templates.js:190:23)
    at run (/Users/christophe.taylor/nurx/deploy/bin/build-deploy.js:240:32)

Expected behavior Expected to be rendered as less than a minute ago

Information dayjs v1.10.5 (also happened in v1.10.4)

const dayjs = require('dayjs');
const relativeTime = require('dayjs/plugin/relativeTime');

dayjs.extend(relativeTime, {
  thresholds: [
    { l: 's', r: 1 },
    { l: 'ss', r: 59, d: 'second' }, // ? 'ss' format does not exist on locale
    { l: 'm', r: 1 },
    { l: 'mm', r: 120, d: 'minute' },
    { l: 'h', r: 1 },
    { l: 'hh', r: 23, d: 'hour' },
    { l: 'd', r: 1 },
    { l: 'dd', r: 29, d: 'day' },
    { l: 'M', r: 1 },
    { l: 'MM', r: 11, d: 'month' },
    { l: 'y' },
    { l: 'yy', d: 'year' }
  ],
});

  const test_date = new Date().getTime() - 30000;
  console.log(dayjs(new Date(test_date)).fromNow(true)); // -> asynchronous error TypeError: v is not a function

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:6

github_iconTop GitHub Comments

5reactions
federaljulescommented, Jun 10, 2022

Got my issue solved. Not sure if it is related to this issue but it had a similar error message. In my case it was because I was updating only some of the default locale settings for relativeTime like this:

dayjs.updateLocale('en', {
	relativeTime: {
		d: '%d d',
		dd: '%d d',
		M: '%d mo.',
		MM: '%d mos.',
		y: '%d y',
		yy: '%d y',
	}
});

I figured out that if you don’t set ALL of the locale options for the relativeTime it will throw this error (or at least very similar error) when dayjs tries to use one of the relativeTime locale options that you didn’t set on updateLocale and doesn’t find it.

This is because updateLocale will replace the default relativeTime locale object with the object you set in the updateLocale. So for example in this case it would not have locale options for h, hh, s, ss etc.

The way I fixed this in my use case was to get the default relativeTime locale values and just spread those and update the ones I need to update.

const localeList = dayjs.Ls;

dayjs.updateLocale('en', {
	relativeTime: {
		...localeList['en'].relativeTime,
		d: '%d d',
		dd: '%d d',
		M: '%d mo.',
		MM: '%d mos.',
		y: '%d y',
		yy: '%d y',
	}
});

Hope this helps someone else as well.

0reactions
federaljulescommented, Jun 10, 2022

Also facing this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

relative time plugin throws: asynchronous error TypeError: v is not a ...
Describe the bug Trying to use relative time formatting for relative times less than a minute throws an error: asynchronous error TypeError: v...
Read more >
TypeError: Object(...) is not a function in Vue - Stack Overflow
The problem is your call to scheduleMeeting in your createMeeting method, or more precicely that you have not actually imported a function, ...
Read more >
CoffeeScript
A CoffeeScript => becomes a JS => , a CoffeeScript class becomes a JS class and so on. Major new features in CoffeeScript...
Read more >
Errors | Node.js v19.3.0 Documentation
This will not work because the callback function passed to fs.readFile() is called asynchronously. By the time the callback has been called, the...
Read more >
chrome.scripting - Chrome Developers
Files are specified as strings that are paths relative to the extension's root ... For instance, the following code will not work, and...
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