I can't figure out how to include JavaScript packages in my Laravel project. I've been trying to install packages, and I can't figure out how it works. For example: Let's say I want to install Bootstrap Confirmation (http://bootstrap-confirmation.js.org/).
I install it from my project root folder.
$ npm install bootstrap-confirmation2
After that, the package.json file looks like the following.
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.18",
"bootstrap": "^4.0.0",
"cross-env": "^5.2.0",
"jquery": "^3.2",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.5",
"popper.js": "^1.15.0",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
},
"dependencies": {
"#types/bootbox": "^4.4.36",
"bootbox": "^5.1.2",
"bootstrap-confirmation2": "^4.1.0"
}
}
I then add the package to app.js.
require('bootstrap-confirmation2');
And my webpack.mix.js looks like this:
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
After that, I run this from my console:
npm run dev
If I try to use my Bootstrap confirmation package it doesn't work, no errors, nothing.
<button class="btn btn-default" data-toggle="confirmation">Confirmation</button>
I know the problem is not in the package but in the way I try to install packages and including them in my app.js file that is loaded once I open the app.
I checked the 'Network' tab in Chrome dev tools, checked that 'app.js' is loaded, searched the file and the package I installed and included is INSIDE the JS file and it still doesn't work.
Any help is appreciated.
In the terminal, run the following install.
npm install bootstrap-confirmation2
The package.json should now look something like the following. Dependencies are jQuery, Popper.js (for Bootstrap 4), and of course, Bootstrap 4.
"dependencies": {
"bootstrap": "^4.3.1",
"bootstrap-confirmation2": "^4.1.0",
"jquery": "^3.4.0",
"laravel-mix": "^4.0.15",
"popper.js": "^1.15.0"
webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
app.css
// Variables
#import 'variables';
// Bootstrap
#import '~bootstrap/scss/bootstrap';
app.js
try {
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');
require('bootstrap-confirmation2');
} catch (e) {}
In your Blade template/layout:
<a class="btn btn-large btn-primary" data-toggle="confirmation" data-title="Open Google?"
href="https://google.com" target="_blank">Confirmation</a>
At the end of the file, after the closing body tag:
</body>
<script src="{{ mix('js/app.js') }}"></script>
In app.js...
$('[data-toggle=confirmation]').confirmation({
rootSelector: '[data-toggle=confirmation]',
// other options
});
At the terminal:
npm run prod
I hope that helped.
Related
I have followed the steps mentioned in this link for desktop notification in localhost https://pusher.com/tutorials/desktop-notifications-laravel/. and run the command in cli 'npm run dev'.
after running the project using command 'php artisan serve' in live link getting the error
app.js
require('./bootstrap');
Vue.component('home', require('./components/Home.vue'));
webpack.mix.js
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
//
]).vue();
bootstrap.js
window._ = require('lodash');
/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allows your team to easily build robust real-time web applications.
*/
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true
});
welcome.blade.php
<!-- ./resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>News Talk</title>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<style>
.container {
padding-top: 100px;
}
</style>
<!-- Scripts -->
<script>
window.Laravel = {!! json_encode([
'csrfToken' => csrf_token(),
]) !!};
</script>
</head>
<body>
<div id="app">
<!-- home Vue component -->
<home></home>
</div>
<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.12/vue.js"></script>
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
when run the command 'npm run dev'
package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production"
},
"devDependencies": {
"axios": "^0.21",
"laravel-mix": "^6.0.6",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"vue-loader": "^15.9.7",
"vue-template-compiler": "^2.6.12"
},
"dependencies": {
"laravel-echo": "^1.11.3",
"pusher-js": "^7.0.3",
"vue": "^2.6.12"
}
}
Please help me to solve this issue.
First remove node_modules folder and package-lock file by running rm -rf node_modules/ package-lock.json.
Remove vue dependencies vue, vue-loader and vue-template-compiler from package.json file.
Now run npm install.
Install vue and vue-loader by npm install vue vue-loader file-loader
It will install vue 2.6.13 and vue-loader 15.9.7 and file-loader 6.2.0 version package.
Install npm install vue-template-compiler --save
In app.js file
require('./bootstrap');
import Vue from 'vue';
Vue.component('home', require('./components/Home.vue'));
You don't need to add cdn link in blade file. It will automatically added using webpack
you have missing this window.Vue = require("vue"); in
app.js
window.Vue = require("vue");
require('./bootstrap');
Vue.component('home', require('./components/Home.vue'));
put this on your package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production"
},
"devDependencies": {
"axios": "^0.21",
"laravel-mix": "^6.0.6",
"lodash": "^4.17.19",
"postcss": "^8.1.14",
"vue-loader": "^15.9.7",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
},
"dependencies": {
"laravel-echo": "^1.11.3",
"pusher-js": "^7.0.3",
"vue": "^2.6.12"
}
}
then run npm run update && npm run dev
I've downloaded Video.js using npm, but I'm having trouble getting it imported to use it on the front end. Here are the steps I've taken:
Installed Video.js with npm, and the /node_modules directory was created.
Added code to webpack.mix.js at the beginning of the file.
webpack.mix.js
const mix = require('laravel-mix');
const path = require('path');
mix.webpackConfig({
resolve: {
alias: {
'videojs': path.resolve(__dirname, 'node_modules/video.js/dist/video.min.js'),
}
}
});
Ran npm run dev successfully.
added below code to view play.blade.php:
play.blade.php
<script type="module">
import 'videojs';
videojs('my-player');
</script>
I get this error Uncaught TypeError: Failed to resolve module specifier "videojs". Relative references must start with either "/", "./", or "../".
Why is it searching for a path when I'm using an alias?
I've tried so many variations of getting the package to import and no success, been stuck for 2 days. How do I solve this? I want to use video.js in the view.
First, install the latest version of Video.js.
npm install --D video.js
In /resources/js/bootstrap.js add...
window._ = require('lodash');
require('video.js');
Compile your assets...
npm run prod
Add the following to your layout.
<script src="{{ asset('js/app.js') }}"></script>
For reference here's my package.json.
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production"
},
"devDependencies": {
"axios": "^0.21",
"laravel-mix": "^6.0.18",
"lodash": "^4.17.21",
"postcss": "^8.2.13",
"video.js": "^7.11.8"
}
}
And also webpack.mix.js.
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
//
]);
Then, in the player.
<video
id="my-video"
class="video-js"
controls
preload="auto"
width="640"
data-setup="{}">
<p class="vjs-no-js">
To view this video please enable JavaScript,
and consider upgrading to a web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank">
supports HTML5 video
</a>
</p>
</video>
If you want to use the CSS. then include in /resources/css/app.css:
#import "~video.js/dist/video-js";
Laravel was installed and so was Vue:
composer require laravel/ui
php artisan ui vue
npm install && npm run dev`
However, Laravel cannot find the Vue instance that is created in app.js.
welcome.blade.php:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<script src="js/app.js"></script>
</head>
<div id="app">
<example-component></example-component>
</div>
<script>
</script>
</body>
</html>
The error that appears in the browser console showing that Laravel can't fine the Vue instance named #app:
[Vue warn]: Cannot find element: #app
Any help would be greatly appreciated:
app.js
require('./bootstrap');
window.Vue = require('vue');
Vue.component('example-component',require('./components/ExampleComponent.vue').default);
const app = new Vue({
el: '#app',
});
package.json:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.19",
"bootstrap": "^4.0.0",
"cross-env": "^7.0",
"jquery": "^3.2",
"laravel-mix": "^5.0.1",
"lodash": "^4.17.13",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.20.1",
"sass-loader": "^8.0.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
}
}
The only way I was able to get Vue working on Laravel was to use this as the correct example:
https://github.com/connor11528/laravel-vue-spa-tailwindcss
I'm trying to integrate Core-ui PRo with Laravel with no luck. I get a blank page. No errors in console, no errors compiling. I don't have experience installing this templates.
In the Vue tab I can see the root but nothing below. Inside the root have this data:
route
path:"/dashboard"
query:Object (empty)
params:Object (empty)
fullPath:"/dashboard"
name:"Dashboard"
meta:Object (empty)
My packages.json:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^3.8.0",
"#vue/cli-plugin-e2e-nightwatch": "^3.8.0",
"#vue/cli-plugin-eslint": "^3.8.0",
"#vue/cli-plugin-unit-jest": "^3.8.0",
"#vue/cli-service": "^3.8.4",
"#vue/test-utils": "1.0.0-beta.29",
"axios": "^0.19",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"cross-env": "^5.1",
"growl": "^1.10.5",
"https-proxy-agent": "^2.2.1",
"laravel-mix": "^5.0.1",
"lodash": "^4.17.13",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^8.0.0",
"vue-template-compiler": "^2.6.11"
},
"dependencies": {
"#babel/polyfill": "^7.4.4",
"#coreui/coreui-plugin-chartjs-custom-tooltips": "^1.3.1",
"#coreui/coreui-pro": "^2.1.14",
"#coreui/icons": "0.3.0",
"#coreui/vue": "^2.1.2",
"bootstrap": "^4.3.1",
"bootstrap-vue": "2.0.0-rc.24",
"chart.js": "^2.8.0",
"core-js": "^2.6.9",
"css-vars-ponyfill": "^2.0.2",
"flag-icon-css": "^3.3.0",
"font-awesome": "^4.7.0",
"mini-toastr": "0.6.6",
"perfect-scrollbar": "^1.4.0",
"quill": "^1.3.6",
"simple-line-icons": "^2.4.1",
"spinkit": "1.2.5",
"text-mask-addons": "^3.8.0",
"v-calendar": "^0.9.7",
"vue": "^2.6.10",
"vue-chartjs": "^3.4.2",
"vue-codemirror": "^4.0.6",
"vue-grid-layout": "^2.3.4",
"vue-mq": "^1.0.1",
"vue-multiselect": "^2.1.6",
"vue-notifications": "0.9.0",
"vue-perfect-scrollbar": "^0.1.0",
"vue-quill-editor": "^3.0.6",
"vue-resize": "^0.4.5",
"vue-router": "^3.0.6",
"vue-select": "2.4.0",
"vue-simple-calendar": "^3.0.2",
"vue-tables-2": "^1.4.70",
"vue-text-mask": "^6.1.2",
"vue2-google-maps": "^0.10.6",
"vuelidate": "^0.7.4"
}
}
My app.scss
// CoreUI Icons Set
#import '~#coreui/icons/css/coreui-icons.min.css';
/* Import Font Awesome Icons Set */
$fa-font-path: '~font-awesome/fonts/';
#import '~font-awesome/scss/font-awesome.scss';
/* Import Simple Line Icons Set */
$simple-line-font-path: '~simple-line-icons/fonts/';
#import '~simple-line-icons/scss/simple-line-icons.scss';
/* Import Flag Icons Set */
#import '~flag-icon-css/css/flag-icon.min.css';
/* Import Bootstrap Vue Styles */
#import '~bootstrap-vue/dist/bootstrap-vue.css';
// Import Main styles for this application
#import '../js/assets/scss/style';
My app.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import './polyfill'
// import cssVars from 'css-vars-ponyfill'
import Vue from 'vue'
import BootstrapVue, { BvComponent } from 'bootstrap-vue'
import router from './router/index'
import coreui from '#coreui/coreui-pro'
// todo
// cssVars()
Vue.use(BootstrapVue)
Vue.component('dashboard', require('./views/Dashboard').default);
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
coreui,
dashboard
})
And copied all folders inside scr from core-ui package
And this is my blade file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>CoreUI - Vue Open Source Bootstrap Admin Template</title>
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<script>
window.Laravel = {!! json_encode([
'csrfToken' => csrf_token(),
]) !!};
</script>
</head>
<!-- BODY options, add following classes to body to change options
// Header options
1. '.header-fixed' - Fixed Header
// Brand options
1. '.brand-minimized' - Minimized brand (Only symbol)
// Sidebar options
1. '.sidebar-fixed' - Fixed Sidebar
2. '.sidebar-hidden' - Hidden Sidebar
3. '.sidebar-off-canvas' - Off Canvas Sidebar
4. '.sidebar-minimized' - Minimized Sidebar (Only icons)
5. '.sidebar-compact' - Compact Sidebar
// Aside options
1. '.aside-menu-fixed' - Fixed Aside Menu
2. '.aside-menu-hidden' - Hidden Aside Menu
3. '.aside-menu-off-canvas' - Off Canvas Aside Menu
// Breadcrumb options
1. '.breadcrumb-fixed' - Fixed Breadcrumb
// Footer options
1. '.footer-fixed' - Fixed footer
-->
<body>
<noscript>
<strong>We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app">
<dashboard></dashboard>
</div>
<!-- built files will be auto injected -->
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
I am using Laravel 5.8 which includes Vue JS in it by default and I want to use Vuetify. Here is what I have done
I have followed exactly what is written in the blog https://codersdiaries.com/laravel-vuetify/ and I am getting an error message in the console that [Vue warn]: Error in beforeCreate hook: "Error: Vuetify is not properly initialized
Here are my files
welcome.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link href='https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons' rel="stylesheet">
<script src="{{ asset('js/app.js') }}" defer></script>
</head>
<body>
<v-app id="app">
<h1>Test Vuetify</h1>
</v-app>
</body>
</html>
app.js
window.Vue = require('vue');
import Vuetify from 'vuetify'
Vue.use(Vuetify)
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
const app = new Vue({
el: '#app',
});
package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.19",
"cross-env": "^5.1",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.13",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
},
"dependencies": {
"vuetify": "^2.0.18"
}
}
You aren’t giving Vue an instance of Vuetify:
const vuetify = new Vuetify();
const app = new Vue({
el: '#app',
vuetify
});