22

This is my logger.ts file that error happens there:

import logger from "pino";
import dayjs from "dayjs";

const log = logger({
  prettyPrint: true,
  base: {
    pid: false,
  },
  timestamp: () => `,"time":"${dayjs().format()}"`,
});

export default log;

ERROR:

yarn dev
yarn run v1.22.17
warning package.json: License should be a valid SPDX license expression
warning ..\..\..\..\package.json: No license field
$ ts-node-dev --respawn --transpile-only src/app.ts
[INFO] 19:09:45 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.6.3)
TypeError: (0 , dayjs_1.default) is not a function
    at timestamp (C:\Users\A\Desktop\desktop\base_code\back_bc_node\src\utils\logger.ts:9:37)
    at pino (C:\Users\A\Desktop\desktop\base_code\back_bc_node\node_modules\pino\pino.js:147:26)
    at Object.<anonymous> (C:\Users\A\Desktop\desktop\base_code\back_bc_node\src\utils\logger.ts:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Module._compile (C:\Users\A\Desktop\desktop\base_code\back_bc_node\node_modules\source-map-support\source-map-support.js:568:25)
    at Module.m._compile (C:\Users\A\AppData\Local\Temp\ts-node-dev-hook-15803171947249184.js:69:33)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at require.extensions.<computed> (C:\Users\A\AppData\Local\Temp\ts-node-dev-hook-15803171947249184.js:71:20)
    at Object.nodeDevHook [as .ts] (C:\Users\A\Desktop\desktop\base_code\back_bc_node\node_modules\ts-node-dev\lib\hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:981:32)
[ERROR] 19:09:46 TypeError: (0 , dayjs_1.default) is not a function
(node:10308) [PINODEP008] PinoWarning: prettyPrint is deprecated, look at https://github.com/pinojs/pino-pretty for alternatives.
(Use `node --trace-warnings ...` to show where the warning was created)

It was working on my previous app, but I don't know why do I get this error for my new application?

6 Answers 6

41

dayjs does not export a default entry like that; use import * as dayjs from 'dayjs' to import all of it's exports (as you are trying to do)

5
  • thanks, this helped me as well - any idea why the normal default import works in my react frontend (import dayjs from "dayjs";) but not in my Nest backend?
    – mike87
    Commented Nov 24, 2023 at 10:39
  • @mike87 Are you using the same version of dayjs on both sides? One may be using an older version (e.g. one that was built for CJS)
    – 0xLogN
    Commented Nov 24, 2023 at 22:02
  • yes, same version, using "dayjs": "^1.11.10" in both frontend and backend
    – mike87
    Commented Nov 27, 2023 at 9:24
  • 1
    @mike87 I mean the exact version, not the version found in package.json. This can be found with the npm list dayjs command.
    – 0xLogN
    Commented Nov 29, 2023 at 5:42
  • oh alright sorry - in my backend the output is [email protected], in my client it's a much longer output, but still it shows the same version: ├─┬ @ant-design/[email protected] │ └── [email protected] deduped ├─┬ @ant-design/[email protected] │ └─┬ @ant-design/[email protected] │ └── [email protected] deduped ├─┬ @ant-design/[email protected] │ ├─┬ @ant-design/[email protected] │ │ └── [email protected] deduped │ ├─┬ @ant-design/[email protected] │ │ └── [email protected] deduped │ ├─┬ @ant-design/[email protected] │ │ └── [email protected] deduped
    – mike87
    Commented Nov 30, 2023 at 11:34
15

I have solved the same problem by adding "esModuleInterop": true to tsconfig.json:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es6"
    }
}
2
  • I tried this and it was creating js files for every ts file in the src directory. Not sure why but I had to remove and had to use use import * as dayjs from 'dayjs'.
    – FAntony
    Commented Mar 8, 2023 at 0:08
  • @FAntony That tsconfig.json is set up to create the dist files in the same directory, it'd be possible to add esModuleInterop to your existing tsconfig however
    – 0xLogN
    Commented Nov 24, 2023 at 22:03
5

When I change the import to

import * as dayjs from 'dayjs'

the node runs normally, but the typeScript throws an error. Changing the import to

import dayjs = require('dayjs');

solved the problem.

1
  • ms library'type definitions doesnt use namespace and hence cant use "* as" syntax, using require worked Commented Jun 12 at 9:47
0

well two solutions suffice

  1. Adding "esModuleInterop": true to the ts.config file
  2. Changing the import to import dayjs = require('dayjs') The former seems to be better solution since it keeps your code structure and maintains the es-modules import structure pattern.
0

I tried adding esModuleInterop: true to the tsconfig.json file, and it worked for me. My error was similar to the above: TypeError: (0 , cors_1.default) is not a function.

0

So I got this error TypeError: (0 , dayjs_1.default) while testing my angular application using Jest.

I had to change the import from import dayjs from 'dayjs' to import * as dayjs from 'dayjs' and it fixed.

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.