I'm trying to send assets from an npm package to a nuxt application. Has anyone had success doing this?
I have an vue component as an npm package with stucture:
-src/
-assets/
-noun-filter.svg
This vector image is loaded in the vue component's template in this npm package like so:
<img class="filter-icon" :src="require('../assets/noun-filter.svg')"/>
The above works fine for use when testing this package on it's own.
However when I import the npm package in a nuxt app like so:
<script>
export default {
name: "NuxtMainPage",
components: {
NpmImportedComponent: process.client
? () => import("@myname/mynpmpackage").then(m => m.Content)
: null
},
//...
}
</script>
When I run the app I get a 404 for the svg.
Nuxt looks for the file in http://localhost:3000/_nuxt/img/noun-filter.b16c5666.svg
where it does not exist.
Is there something I'm missing here? Why does nuxt change the path of the assets? Why is it not just looking in the node modules like we would expect?
How does one send static assets via an npm package to nuxt?
What I've tried
- Tried using url-loader in my app. Url's were still getting redirected in nuxt
- Import items with
import @myname/mypackage/src/assets/noun-filter.svg
in the nuxt app - Switch the relative urls in npm package from
../assets
to@assets
- Move the assets folder to the root of the npm package and add it to included files in package.json
All of the above had the same result with nuxt looking for the image at:
http://localhost:3000/_nuxt/img/noun-filter.b16c5666.svg
I think there is no easy solution here.
First solution, use
url-loader
to inline your asset file as a base-64 data url. See Asset URL Handling.vue.config.js
Second solution, copy your asset file to
static
directory and use absolute url instead.Related post.