How to add external js file in Nuxt?

13,696

Simply keep your script.js file into static folder, because static folder treat as root folder.

and add the path into nuxt.config.js configuration file like below

script: [
          { src: '/script.js'}
      ]
Share:
13,696
Admin
Author by

Admin

Updated on August 16, 2022

Comments

  • Admin
    Admin over 1 year

    I have js file called script.js in assets/js. Tried to include in nuxt.config.js like below:

    head: {
        title: pkg.name,
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          { hid: 'description', name: 'description', content: pkg.description }
        ],
        link: [
          { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
        ],
          script: [
              { src: '/assets/js/script.js', ssr: false }
          ]
      }
    

    But getting 'GET http://127.0.0.1:3000/assets/js/script.js 404 (OK)'.

  • FooBar
    FooBar about 4 years
    This does not work if you use the router to redirect to a page.