I tried to add an external javascript file to nuxt.config.ts. but nuxt didn't load the file.
nuxt.config.ts file:
export default defineNuxtConfig({
css: [
'bootstrap/dist/css/bootstrap.min.css'
],
script: [
{
src: 'script.js',
}
],
vite: {
define: {
'process.env.DEBUG': false,
},
}
})
I tried to add an external javascript file to nuxt.config.ts. but nuxt didn't load the file.
nuxt.config.ts file:
export default defineNuxtConfig({
css: [
'bootstrap/dist/css/bootstrap.min.css'
],
script: [
{
src: 'script.js',
}
],
vite: {
define: {
'process.env.DEBUG': false,
},
}
})
Share
Improve this question
edited Nov 8, 2022 at 12:28
kissu
46.7k16 gold badges89 silver badges188 bronze badges
asked Nov 8, 2022 at 8:04
diyifi3030diyifi3030
931 gold badge1 silver badge6 bronze badges
5
|
2 Answers
Reset to default 12The correct way to load scripts in Nuxt 3 is by using the head
object in app
(See more in Official Docs here)
In your case, This should work.
export default defineNuxtConfig({
app: {
head: {
link: [
{ rel: 'stylesheet', href: 'bootstrap/dist/css/bootstrap.min.css' },
],
script: [{ src: 'script.js' }],
},
},
vite: {
define: { 'process.env.DEBUG': false },
},
});
head
contain meta
, script
, link
, style
and no-script
tags, so if you want to write any of them you'll use the same approach
I tried many ways and this is the only way it's worked.
export default defineNuxtConfig({
css: [
'@/css/bootstrap.rtl.min.css'
],
app: {
head: {
script: [
{
src: '_nuxt/js/bootstrap.bundle.min.js',
}
],
}
}
})
public
? Tried with/script.js
? – kissu Commented Nov 8, 2022 at 8:16script
should be inside ofmeta
. – kissu Commented Nov 8, 2022 at 10:04