Let's start with jQuery. I first ran:
npm install jquery-ui --save-dev
Then in my own file custom.js I added the following lines:
import $ from 'jquery';
$('#logo').on('click', function(){
alert("Hello")
})
In the main html file I imported the custom js file
<script src="{{ asset('js/custom.js') }}" type="module"></script>
Does not work and I get the error: Uncaught TypeError: Error resolving module specifier “jquery”. Relative module specifiers must start with “./”, “../” or “/”.
If I remove the type=module from the main html file I get a different error: Uncaught SyntaxError: import declarations may only appear at top level of a module.
What am I doing wrong and what step miss to install jQuery so it can work pls?
Next I tried Alpine JS
I cleared my custom.js file and tried to install alpine js doing the following:
npm install alpinejs
then in my empty custom.js file:
import Alpine from 'alpinejs'
window.Alpine = Alpine
Alpine.start()
Then I ran
npm run dev
In the main html file I added the script tag
<script src="{{ asset('js/app.js') }}" defer></script>
and in the body to test if it works I added:
<div x-data="{ show: false }">
<button @click="show = !show">Show</button>
<h1 x-show="show">Alpine Js is working !</h1>
</div>
<hr>
<div x-data>
<button @click="alert('Alpine Js is working !')">Click</button>
</div>
If I click the button I get the following error in the console:
[Vue warn]: Property or method "alert" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
(found in <Root>) 2 app.js:28785:15
[Vue warn]: Error in v-on handler: "TypeError: alert is not a function"
(found in <Root>) app.js:28785:15
TypeError: alert is not a function
click http://127.0.0.1:8000/js/app.js line 39872 > Function:3
invokeWithErrorHandling http://127.0.0.1:8000/js/app.js:30027
invoker http://127.0.0.1:8000/js/app.js:30352
_wrapper http://127.0.0.1:8000/js/app.js:35755
add$1 http://127.0.0.1:8000/js/app.js:35759
updateListeners http://127.0.0.1:8000/js/app.js:30384
updateDOMListeners http://127.0.0.1:8000/js/app.js:35789
invokeCreateHooks http://127.0.0.1:8000/js/app.js:34270
createElm http://127.0.0.1:8000/js/app.js:34157
createChildren http://127.0.0.1:8000/js/app.js:34254
createElm http://127.0.0.1:8000/js/app.js:34155
createChildren http://127.0.0.1:8000/js/app.js:34254
createElm http://127.0.0.1:8000/js/app.js:34155
patch http://127.0.0.1:8000/js/app.js:34717
_update http://127.0.0.1:8000/js/app.js:32119
updateComponent http://127.0.0.1:8000/js/app.js:32240
get http://127.0.0.1:8000/js/app.js:32654
I know I can just add a script line with a CDN with either of the scripts I need, but I would really like to know why this is not working as I followed the instructions carefully on more than one website.