ReferenceError: dayjs is not defined in Vite with Laravel
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
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
{
"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
<head>
...
@vite('resources/js/app.js')
</head>
<body>
<script>
console.log(dayjs());
</script>
</body>
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
Hi, I'm not familiar with laravel, but why does lodash and axios are devDependecies?
I have no idea, that's how npm chose to install them
Try to move them in dependencies and check if problem is solved
Try to move them in dependencies and check if problem is solved
I put it in devDependencies but it doesn't work. I had the same problem with MomentJs. MomentJs works in devDependencies, but I don't want to use MomentJs
DevDependencies are dependencies that needed only during development. Moment (axios and lodash) should be production dependency