iamkun / iamkun/dayjs

ReferenceError: dayjs is not defined in Vite with Laravel

Open
#2,020 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

I'm trying to import the **dayjs** library via npm install in my laravel project that builds with vite.js
In the console I get this error: **ReferenceError: dayjs is not defined**

vite.config.js
``` js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

import dayjs from 'dayjs';

export default defineConfig({
plugins: [
laravel([
'resources/css/app.css',
'resources/js/app.js',
]),
],
});
```

package.json
```json
{
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"axios": "^0.25",
"laravel-vite-plugin": "^0.2.1",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"sass": "^1.53.0",
"vite": "^2.9.11"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^6.1.1",
"@popperjs/core": "^2.11.5",
"bootstrap": "^5.1.3",
"bootstrap-icons": "^1.9.0",
"dayjs": "^1.11.4",
}
}

```

in welcome.blade.php

```html

...
@vite('resources/js/app.js')


console.log(dayjs());

```

after running `npm run dev`, everything is ok, I have no errors

**Desktop:**
- OS: Windows 10
- Browser: Chrome Version 103.0.5060.134
- Version laravel: 9.19
- Version dayjs: 1.11.4

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.