1

I tried all the ways to import the JS file and when i tried to navigate the script from the view source code it gives error "/* script not found */" .
i need to know what is the correct way to import local js scripts .
below is the nuxt.config.js file .

head: {
  title: 'admin',
  htmlAttrs: {
    lang: 'en'
  },
  meta: [
    { charset: 'utf-8' },
    { name: 'viewport', content: 'width=device-width, initial-scale=1' },
    { hid: 'description', name: 'description', content: '' },
    { name: 'format-detection', content: 'telephone=no' }
  ],
  link: [
    { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
  ],
  script: [
    {
      type: 'text/javascript',
      src: './assets/js/dashboard.js'
    },
    {
      type: 'text/javascript',
      src: '~/assets/plugins/charts-c3/plugin.js'
    },
    {
      type: 'text/javascript',
      src: '@/assets/plugins/maps-google/plugin.js'
    },
    {
      type: 'text/javascript',
      src: '/assets/plugins/input-mask/plugin.js'
    }
  ],
},
4
  • 2
    Did you tried importing it in plugins rather than script? nuxtjs.org/docs/2.x/directory-structure/… Commented Aug 18, 2021 at 11:37
  • actually it worked but its not in header Commented Aug 18, 2021 at 11:46
  • Why do you want it in the header? It's not really optimized. Commented Aug 18, 2021 at 12:04
  • If you really want it in head, look at this answer: stackoverflow.com/a/67535277/8816585 Commented Aug 18, 2021 at 12:05

3 Answers 3

1

You can use the plugins key to import it globally to your Nuxt app (be careful of performance issues tho)

nuxt.config.js

export default {
  plugins: ['~/plugins/vue-tooltip.js']
}

More info available here: https://nuxtjs.org/docs/2.x/directory-structure/plugins#the-plugins-property


If you want to use some 3rd party scripts that should be available in the header (which is not the case from the code you shared so far), you could look into this answer

nuxt.config.js

export default {
  head: {
    script: [
      { hid: 'stripe', src: 'https://js.stripe.com/v3/', defer: true }
    ]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe take a look at Nuxt plugin property

You can use the script property for externally available scripts (try to put it in your static directory and add it like it : ./my-scripts.js

1 Comment

i tried the plugin property but its not inserting the script in header i will try the static directory
0

I am using Nuxt v3 RC and updating nuxt.config.js did not help when using NuxtLayout. I had to add the head to the default.vue layout to get external libraries to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.