kit: Can't import dayjs on dev server
Edit: Fixed by specifically importing the the ES module
import dayjs from "dayjs/esm";
import relativeTime from "dayjs/esm/plugin/relativeTime";
Original bug report
Describe the bug
We have a basic component that imports dayjs
, but there is an error displaying a page with this component when using npm run dev
. Running in production with npm run build && npm start
works fine.
The component:
<script>
import dayjs from 'dayjs';
const date = dayjs().format('YYYY-MM-DD');
</script>
<header>
<div>
<time datetime="{date}">
{date}
</time>
</div>
</header>
Logs Browser logs
500
__vite_ssr_import_1__.default is not a function
TypeError: __vite_ssr_import_1__.default is not a function
at HeaderTop.svelte:3:22
at Object.$$render (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/node_modules/svelte/internal/index.js:1369:22)
at eval (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/src/routes/index.svelte:15:103)
at Object.$$render (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/node_modules/svelte/internal/index.js:1369:22)
at Object.default (root.svelte:46:46)
at eval (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/src/routes/$layout.svelte:11:41)
at Object.$$render (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/node_modules/svelte/internal/index.js:1369:22)
at root.svelte:42:13
at $$render (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/node_modules/svelte/internal/index.js:1369:22)
at Object.render (/Users/stanislav.khromov/Documents/GitHub/kratos-svelte/node_modules/svelte/internal/index.js:1377:26)
To Reproduce ✅ Please make sure you’ve tested with the latest versions of Svelte and SvelteKit.
Information about your SvelteKit Installation:
- The output of
npx envinfo --system --npmPackages svelte,@sveltejs/kit --binaries --browsers
System:
OS: macOS 10.15.7
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Memory: 1.86 GB / 32.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 15.12.0 - ~/.nvm/versions/node/v15.12.0/bin/node
npm: 7.6.3 - ~/.nvm/versions/node/v15.12.0/bin/npm
Browsers:
Chrome: 89.0.4389.90
Firefox: 86.0.1
Safari: 14.0.3
npmPackages:
@sveltejs/kit: next => 1.0.0-next.61
svelte: ^3.29.0 => 3.35.0
-
Your browser Chrome 89
-
Your adapter (e.g. Node, static, Vercel, Begin, etc…) Node
Severity Blocking SvelteKit usage.
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (3 by maintainers)
same error here too.
Cannot use import statement outside a module
Happens upon npm run build
Strangely enough, when i change everything back and add the .js extension:
it works
@khromov I still get the error. I did not change anything else, just added the
import dayjs from "dayjs/esm"
On
pnpm run dev
:Hey @benmccann ! I added the solution at the top of my original post before closing, but I’ll post it here again for the “scrollers”. 😄
Fixed by specifically importing the ES module directly
Example:
There are 2 of 30+ plugins already ported in the
dayjs@2.0.0-alpha.2
(as listed here) but the best way to check is to peek inside the NPM package. If you’re going to need plug-in support right away you may want to consider working through the peculiarities of Dayjs v1 integration then upgrade when Dayjs 2 hits beta. For Dayjs v1 I personally had success using the bottom pattern shared by @eikaramba above. Example implementation can be found here. Good luck!@NoelJacob This sounds like it might be a different issue.
Make sure you are running the latest version of Node (v15) which has support for ES Modules, try using
npm
instead ofpnpm
, and try making a completely new SvelteKit project without any other modifications other than importingdayjs
.Since you’re on Windows I can also suggest using Ubuntu or Debian on WSL2 instead of a native Window environment!