dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

ReferenceError: dayjs is not defined in Vite with Laravel

Open csteri opened this issue 3 years ago • 5 comments

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

csteri avatar Aug 08 '22 14:08 csteri

Hi, I'm not familiar with laravel, but why does lodash and axios are devDependecies?

Bykiev avatar Aug 11 '22 09:08 Bykiev

I have no idea, that's how npm chose to install them

csteri avatar Aug 11 '22 12:08 csteri

Try to move them in dependencies and check if problem is solved

Bykiev avatar Aug 11 '22 12:08 Bykiev

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

csteri avatar Aug 11 '22 13:08 csteri

DevDependencies are dependencies that needed only during development. Moment (axios and lodash) should be production dependency

Bykiev avatar Aug 11 '22 18:08 Bykiev