N
Nuxt3mo ago
nvme

copy a folder from node_modules to public folder

how can I do this without manually copying it its required for a sdk im using
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @nvme. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The knowledge sources do not provide a built-in Nuxt command or configuration to automatically copy a folder from node_modules to the public directory during build or development. However, you can achieve this by using a custom script or a build tool (like a postinstall or build hook in your package.json) to copy the required files. For example, you could add a script in your package.json like:
"scripts": {
"copy-sdk": "cp -r node_modules/some-sdk-folder public/some-sdk-folder",
"build": "npm run copy-sdk && nuxt build"
}
"scripts": {
"copy-sdk": "cp -r node_modules/some-sdk-folder public/some-sdk-folder",
"build": "npm run copy-sdk && nuxt build"
}
This approach is not Nuxt-specific but is a common practice in Node.js projects. If you are developing a Nuxt module, you can expose a folder of assets through Nitro’s publicAssets option, which allows you to serve files from a specific directory without copying them manually. Here’s an example from the documentation:
import { defineNuxtModule, createResolver } from '@nuxt/kit'

export default defineNuxtModule({
setup (options, nuxt) {
const resolver = createResolver(import.meta.url)

nuxt.hook('nitro:config', async (nitroConfig) => {
nitroConfig.publicAssets ||= []
nitroConfig.publicAssets.push({
dir: resolver.resolve('./runtime/public'),
maxAge: 60 * 60 * 24 * 365 // 1 year
})
})
}
})
import { defineNuxtModule, createResolver } from '@nuxt/kit'

export default defineNuxtModule({
setup (options, nuxt) {
const resolver = createResolver(import.meta.url)

nuxt.hook('nitro:config', async (nitroConfig) => {
nitroConfig.publicAssets ||= []
nitroConfig.publicAssets.push({
dir: resolver.resolve('./runtime/public'),
maxAge: 60 * 60 * 24 * 365 // 1 year
})
})
}
})
This method is intended for module authors, but it demonstrates how to programmatically expose assets for public access in a Nuxt app Injecting Other Assets. If you need a solution for a regular Nuxt project (not a module), you will need to use a custom script as described above, since the knowledge sources do not mention a built-in Nuxt feature for this use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@1265695120725119036> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?