dwib
dwib

Reputation: 595

Include node module in a standard JS file

I want to include a node module into a standard javascript file using:

const swal = require('sweetalert2');

But this doesn't work, and when i later use the package to do things, it says swal is not defined. I've tried including it with a script tag in my html which also failed:

<script type="module" src="sweetalert2.all.min.js"></script>

that was in the html file that calls the function from the javascript file. Any help on how i can include this package would be greatly apreciated

Upvotes: 0

Views: 289

Answers (2)

Aravind Voggu
Aravind Voggu

Reputation: 1531

If you're trying to use modules installed by npm install in browser, use something like browserify or webpack.

Or use a valid URL

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>

https://www.jsdelivr.com/package/npm/sweetalert2?path=dist

Upvotes: 1

Sachin Shah
Sachin Shah

Reputation: 4533

Install npm module npm i sweetalert2 --save

Try this way to add in js file

// ES6 Modules or TypeScript
import Swal from 'sweetalert2'

// CommonJS
const Swal = require('sweetalert2')

For more info read this link

Upvotes: 0

Related Questions