Can't import dayjs on dev server
See original GitHub issueEdit: 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.
Issue Analytics
- State:
- Created 2 years ago
- Comments:18 (3 by maintainers)
Top Results From Across the Web
SyntaxError: Cannot use import statement outside a module
To use TypeScript imports with Node.js, I installed the below packages. 1. npm i typescript --save-dev 2. npm i ts-node --save-dev.
Read more >dayjs - npm
Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a ...
Read more >JavaScript modules - MDN Web Docs
Use of native JavaScript modules is dependent on the import and export statements; ... you'll need to run them through a local web...
Read more >How to transpile ES modules with webpack and Node.js
See below an example of how to import and export a function with ES module ... npm i webpack webpack-cli webpack-dev-server @babel/core ...
Read more >Import repositories from TFVC to Git - Azure Repos
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server ... and associated history (if imported) cannot exceed 1GB in size....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
: