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.

Can't import dayjs on dev server

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
eikarambacommented, Jul 15, 2021

same error here too. Cannot use import statement outside a module

import dayjs from 'dayjs/esm/index.js'
import isToday from 'dayjs/esm/plugin/isToday/index.js'
import isYesterday from 'dayjs/esm/plugin/isYesterday/index.js'

Happens upon npm run build

Strangely enough, when i change everything back and add the .js extension:

import dayjs from 'dayjs'
import isToday from 'dayjs/plugin/isToday.js'
import isYesterday from 'dayjs/plugin/isYesterday.js'

it works

2reactions
NoelJacobcommented, Mar 27, 2021

@khromov I still get the error. I did not change anything else, just added the import dayjs from "dayjs/esm"

On pnpm run dev:


> windinew@0.0.1 dev C:\Users\HP\Desktop\windinew
> svelte-kit dev

> Listening on http://localhost:3000
4:03:13 pm [vite] Error when evaluating SSR module C:\Users\HP\Desktop\windinew\src\routes\index.svelte:
C:\Users\HP\Desktop\windinew\node_modules\.pnpm\dayjs@1.10.4\node_modules\dayjs\esm\index.js:1
import * as C from './constant';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1024:16)
    at Module._compile (node:internal/modules/cjs/loader:1072:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at nodeRequire (C:\Users\HP\Desktop\windinew\node_modules\.pnpm\vite@2.1.3\node_modules\vite\dist\node\chunks\dep-0776dd57.js:68933:17)
    at ssrImport (C:\Users\HP\Desktop\windinew\node_modules\.pnpm\vite@2.1.3\node_modules\vite\dist\node\chunks\dep-0776dd57.js:68891:20)
    at eval (C:\Users\HP\Desktop\windinew\src\routes\index.svelte:7:31)
 ERROR  Command failed with exit code 1.
Read more comments on GitHub >

github_iconTop 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 >

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