Skip to content

Instantly share code, notes, and snippets.

@ansidev
Last active June 12, 2023 07:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ansidev/46a715dd319cef546646383eac13ec5f to your computer and use it in GitHub Desktop.
Save ansidev/46a715dd319cef546646383eac13ec5f to your computer and use it in GitHub Desktop.
Use jQuery with NuxtJS
const webpack = require('webpack')
module.exports = {
// other configs
build: {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
})
]
},
// other configs
}
{
// other configs.
"dependencies": {
// Replace <jquery.version> with the jquery version you want.
"jquery": "<jquery.version>"
},
// other configs.
}
@ali-master
Copy link

In the webpack v4, the vendor option was removed

@luscas
Copy link

luscas commented Jan 27, 2019

Save jquery.min.js

modify nuxt.config.js

'plugins': [
     { src: '~/plugins/jquery.min.js', ssr: false }
],

@mercyikpe
Copy link

mercyikpe commented Mar 13, 2019

This worked for me @luscas. Thank you.

I needed to add a custom jquery file to nuxt.

here's my nuxt.config.js

plugins: [
{ src: '~/plugins/js/main.js', ssr: false }
],

@developer-syc
Copy link

developer-syc commented Mar 9, 2020

This worked for me @luscas. Thank you.

I needed to add a custom jquery file to nuxt.

here's my nuxt.config.js

plugins: [
{ src: '~/plugins/js/main.js', ssr: false }
],

This solution worked for me like a charm. I had to use the twentytwenty plugin, and so I took the source jquery.min.js file for jquery that was installed in my node_modules folder, and copied it into a folder called ~/plugins/jquery. Then I copied the zurb-twentytwenty node_module files jquery.event.move.js and jquery.twentytwenty.js into the same jquery folder. Then I added the following lines into the plugins section:

`
{ src: '~/plugins/jquery/jquery.min.js', mode: 'client' },

  { src: '~/plugins/jquery/jquery.event.move.js', mode: 'client' },

  { src: '~/plugins/jquery/jquery.twentytwenty.js', mode: 'client' }

`

And it JUST WORKED. No more error messages. I tell you... no other "guide" out in the wild seemed to work at all. I know there's a "vue-twentytwenty" package out there (it didn't work either), however I had use the jQuery version. I haven't fully explored the compatibility and the side effects of using the plugin, but just getting the JS code NOT to error out when jquery is executed is a big win.

@ansidev
Copy link
Author

ansidev commented Mar 10, 2020

In the webpack v4, the vendor option was removed

I 've updated.

@ansidev
Copy link
Author

ansidev commented Mar 10, 2020

Save jquery.min.js

modify nuxt.config.js

'plugins': [
     { src: '~/plugins/jquery.min.js', ssr: false }
],

You should install jquery via npm/yarn. You can update it easier.

@thamerbelfkihthamer
Copy link

there is any way to use jquery within Vuejs components with $, please?

@cokobware
Copy link

there is any way to use jquery within Vuejs components with $, please?

Try this and see if it helps... https://forum.vuejs.org/t/how-to-use-jquery-in-vue-component/45000/5

@SajjadGazery
Copy link

in Nuxt3

First you need to install jquery in your project

npm i jquery

Then in the file ~/plugins/jquery.js add the following codes:

import $ from 'jquery'
window.jQuery = window.$ = $
export default jQuery;

And finally add the following code to the nuxt.config.ts

plugins: [
  { src: "~/plugins/jquery", mode: "client" },
 ],

Sample

<script setup>
   onMounted(()=>{
      console.log($('body'));
   })
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment