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.

Error when loading date-fns dependency: Cannot read property 'default' of null

See original GitHub issue

Describe the bug

I’m using Svelte Kit and Kit uses Vite. After the first start of the project, and the first time visiting the localhost:3000, in the kit console it throws the below log. After second start of project, it doesn’t throw any error. Also, if you delete .svelte and node_modules folders, then start project, it will again throw errors.

Reproduction

I have a public repo for this:

https://github.com/kahverengi001/vite-reproduction-bug-date-fns

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

❯ npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers

  System:
    OS: Linux 5.10 Arch Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-4720HQ CPU @ 2.60GHz
    Memory: 280.51 MB / 15.54 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.14.0 - /usr/bin/node
    Yarn: 1.22.10 - /usr/bin/yarn
    npm: 7.10.0 - /usr/bin/npm
  Browsers:
    Chromium: 90.0.4430.72
    Firefox: 87.0
  npmPackages:
    vite: ^2.1.0 => 2.1.5 

Used package manager: npm/yarn

Logs

❯ yarn dev
yarn run v1.22.10
$ svelte-kit dev

  SvelteKit v1.0.0-next.83

  local:   http://localhost:3000
  network: not exposed
  network: not exposed
  network: not exposed
  network: not exposed

  Use --host to expose server to other devices on this network


11:24:33 AM [vite] new dependencies found: date-fns, updating...
11:24:34 AM [vite] ✨ dependencies updated, reloading page...
Cannot read property 'default' of null
TypeError: Cannot read property 'default' of null
    at eval (/src/lib/Header/index.svelte:24:223)
    at Object.$$render (/home/kahverengi/vite-reproduction-bug-date-fns/node_modules/svelte/internal/index.js:1369:22)
    at eval (/home/kahverengi/vite-reproduction-bug-date-fns/src/routes/$layout.svelte:20:94)
    at Object.$$render (/home/kahverengi/vite-reproduction-bug-date-fns/node_modules/svelte/internal/index.js:1369:22)
    at eval (//home/kahverengi/vite-reproduction-bug-date-fns/.svelte/dev/generated/root.svelte:55:122)
    at $$render (/home/kahverengi/vite-reproduction-bug-date-fns/node_modules/svelte/internal/index.js:1369:22)
    at Object.render (/home/kahverengi/vite-reproduction-bug-date-fns/node_modules/svelte/internal/index.js:1377:26)
    at render_response (file:///home/kahverengi/vite-reproduction-bug-date-fns/node_modules/@sveltejs/kit/dist/ssr.js:364:28)
    at async respond (file:///home/kahverengi/vite-reproduction-bug-date-fns/node_modules/@sveltejs/kit/dist/ssr.js:1110:10)
    at async render_page (file:///home/kahverengi/vite-reproduction-bug-date-fns/node_modules/@sveltejs/kit/dist/ssr.js:1154:20)

Additional Context There is also a tracking current issue in Svelte Kit repo: https://github.com/sveltejs/kit/issues/1111

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mcmxcdevcommented, Apr 20, 2021

I am also using date-fns in my SvelteKit project, try to move the library to devDependencies in package.json Should fix your problem 😉

0reactions
Niputicommented, Oct 31, 2021

removing this part from authors reproduction in svelte.config.cjs makes date-fns work

vite: {
	ssr: {
		noExternal: Object.keys(pkg.dependencies || {})
	}
}

npmPackages: @sveltejs/adapter-node: next => 1.0.0-next.16 @sveltejs/kit: next => 1.0.0-next.83 vite: ^2.1.0 => 2.1.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Can't resolve '@date-io/date-fns'
In my code, I have used "import DateFnsUtils from "@date-io/date-fns/build/date-fns-utils" , which caused "Module not found: Error: Can't ...
Read more >
npm cannot read properties of null (reading 'matches') - You.com ...
I run npm install canvas png-js date-fns axios crypto-js ts-md5 tslib @types/node dotenv ... Cannot read property 'match' of undefined error handling.
Read more >
modern JavaScript date utility library - date-fns
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
Read more >
react-datepicker - npm
If you need to use a locale other than the default en-US, you'll also need to import that into your project from date-fns...
Read more >
Datepicker - Angular Material
A datepicker is composed of a text input and a calendar pop-up, connected via the matDatepicker property on the text input.
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