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.

Issue when using esm import

See original GitHub issue

Describe the bug A clear and concise description of what the bug is. So I have two issue when using “dayjs”: “1.8.14”

The first one if I’m using import like this

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import LocalizedFormat from "dayjs/plugin/localizedFormat";

dayjs.utc(lockedAt).locale(localize) .format("lll");

My code is working perfectly fine but angular Cli will throw error Module “node_modules/dayjs/index” has no default export.

Then I change my code to

import dayjs from "dayjs/esm";
import utc from "dayjs/esm/plugin/utc";
import LocalizedFormat from "dayjs/esm/plugin/localizedFormat";

Then the angular cli not throw any error but the code is not working and I have this error

Cannot read property ‘formats’ of undefined

What have i done wrong.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13

github_iconTop GitHub Comments

27reactions
ngohungphuccommented, Sep 25, 2019

Solve by using “dayjs”: “^1.8.16”

Add this in tsconfig.json

"allowSyntheticDefaultImports": true,

And use like this

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import LocalizedFormat from "dayjs/plugin/localizedFormat";

dayjs.extend(LocalizedFormat);
dayjs.extend(utc);
22reactions
hitautodestructcommented, Jan 9, 2021

If anyone is having a similar issue with node I just had to add the extension to the utc import. My project is vanilla js and it kept trying to import the typescript version of the utc plugin.

I’m running nodejs 14.x

TL;DR

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc.js"; // <------ Notice the .js

dayjs.extend(utc)
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Question] ESM import issues when moving to Angular 13
We are moving to Angular 13 and are now finding that out existing E2E tests (stored in an e2e folder) are failing because...
Read more >
Avoid these issues when using new ECMAScript modules ...
Most typescript applications importing commonjs modules should turn on esModuleInterop in their tsconfig file. Then you can just use a “normal” ...
Read more >
Getting Started with (and Surviving) Node.js ESM
Now let's take a look at actually doing imports in our code. As you'd expect, importing ESM from ESM and CommonJS from CommonJS...
Read more >
require() of ES modules is not supported when importing ...
node-fetch is an ESM-only module - you are not able to import it with require. We recommend you stay on v2 which is...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
The static import declaration is used to import read-only live bindings which are exported by another module. The imported bindings are ...
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