Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference js file from node_modules folder in page head in nuxt.js

I have added package onesignal-emoji-picker to my nuxt.js vue project and I want to reference css and js files in my pages without copying them to static folder. css is included with ease in nuxt.config.js:

css: ['~/node_modules/onesignal-emoji-picker/lib/css/emoji.css']

but I could not manage to reference js files, head.script section seems to work only for external resources:

script: [
  {
    src: 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
    type: 'text/javascript'
  },
  {
    src: '~/node_modules/onesignal-emoji-picker/lib/js/config.js',
    type: 'text/javascript'
  },
  {
    src: '~/node_modules/onesignal-emoji-picker/lib/js/util.js',
    type: 'text/javascript'
  },
  {
    src: '~/node_modules/onesignal-emoji-picker/lib/js/jquery.emojiarea.js',
    type: 'text/javascript'
  },
  {
    src: '~/node_modules/onesignal-emoji-picker/lib/js/emoji-picker.js',
    type: 'text/javascript'
  }
]

enter image description here enter image description here

It seems to me that I should somehow tell webpack to include this files on build and reference them appropriately? How could I do it? Thanks!

like image 858
Andrey Khataev Avatar asked Apr 28 '19 05:04

Andrey Khataev


People also ask

Can I use vue3 with Nuxt?

It's only right that Nuxt 3 was built to support Vue 3 features. Vue 3 was released in October 2020 and has since garnered a lot of praise from the Vue community. With Nuxt 3 being written in Vue 3, you get access to features like the Composition API, better module imports, and overall improved app performance.

How do I change the default port for Nuxt?

If you want to change the port number temporarily, you can do it by adding a --port option to the npm run dev command.


1 Answers

In nuxt.config.js, simply use the plugins key for including any internal scripts:

plugins: [
    { src: "~/node_modules/onesignal-emoji-picker/lib/js/config.js", mode: "client" },
    { src: "~/node_modules/onesignal-emoji-picker/lib/js/util.js", mode: "client" },
    { src: "~/node_modules/onesignal-emoji-picker/lib/js/jquery.emojiarea.js", mode: "client" },
    { src: "~/node_modules/onesignal-emoji-picker/lib/js/emoji-picker.js", mode: "client" }
]

Note the mode: "client" option which means the files will only be run on the client, preventing issues on the server if the library is not compatible with server-side-rendering. You may want to remove the flag in other cases.

like image 76
cprcrack Avatar answered Oct 05 '22 03:10

cprcrack