@captainjackas You don’t need type="module" on the <script> tag.
Laravel + Vite -> ReferenceError: moment is not defined
Hi all,
I am new to Vite and I'm getting pretty frustated trying to add moment.js. I tried numerous suggestions found online but with no result so far, as I'm still receiving the ReferenceError: moment is not defined when I perform a console.log(moment); in my blade. I ran npm install moment@latest --save-dev to install moment.js and npm run build to build the js file added in my blade using @vite(['resources/js/app.js']). Can anyone please help me out here? My setup in as follows:
vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/sass/app.scss',
'resources/js/app.js'
],
refresh: true,
})
],
});
bootstrap.js
import 'bootstrap';
import axios from 'axios';
window.axios = axios;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
import $ from 'jquery';
window.jQuery = window.$ = $;
app.js
import './bootstrap';
import 'owl.carousel';
import 'jquery-ui/dist/jquery-ui';
import DataTable from 'datatables.net-bs5';
DataTable(window, window.$);
import moment from 'moment';
window.moment = moment();
blade.php
<script type="module">
$(document).ready(function () {
console.log(moment);
});
</script>
So apparently the line DataTable(window, window.$); in the app.js file caused the following error: Cannot set property window of #<Window> which has only a getter, which prevent moment to be imported properly. Apparently this line was redundant so after deleting it momentjs was imported properly.
Please or to participate in this conversation.