Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import AlpineJS with NPM?

How is one supposed to import AlpineJS with NPM?

The AlpineJS docs say:

From npm: Install the package from npm.

npm i alpinejs

Include it in your script.

import 'alpinejs'

I put that last line into one of my JavaScript files. Nothing happened though. I figured, maybe I need a bundler like Grunt to make that import actually do something. I wouldn't know how the client should otherwise know that 'alpinejs' refers to alpine.js in node_modules/alpinejes/dist. Grunt just lets the line stand as it is and doesn't the AlpineJS file to my output directory.

What I want to achieve:

  • Use AlpineJS in the client, without being dependent on the CDN
  • Possibly: concatenating alpine.js with other script files to reduce the number of server requests
like image 269
kslstn Avatar asked Oct 20 '25 17:10

kslstn


1 Answers

Option one

import Alpine from 'alpinejs'
window.Alpine = Alpine
Alpine.start();

Best option for me

import Alpine from 'alpinejs';
window.Alpine = Alpine;
queueMicrotask(() => {
    Alpine.start()
});
like image 113
sahed moral Avatar answered Oct 24 '25 11:10

sahed moral