Images uploaded in Vue.js production mode not showing

Viewed 1351

I am a bit new to Vue.js. I am doing a social media application that allows users to upload and share images with others. I store my images in src/assets folder during development. However, when I build the project, all images are put in the dist folder. Therefore, what can I do to enable users to still upload images on production? Do I create the assets directory in the dist folder? I have since tried different ways, including storing images on the backend. In dooing this, I reference the backend path relatively, using, for example, ../../../backend/public/assets..., and it works on development. However, when I build, the images that existed in the backend directory at the time of building are visible, however, whenever I try uploading more on production to the ../../../backend/public/assets... directory, they are uploaded successfully but are not visible (that is on production). I get an error that Cannot find module './image_name.image_extension'. What am I doing wrong? I have seen similar questions like this but there was no answer.

1 Answers

You must set your public path and change your way!!

first step to do is creating vue.config.js in your root directory, if you want to know more details, read this: https://cli.vuejs.org/config/

for example, I define prefix path for my files:

module.exports = {
  publicPath:
    process.env.NODE_ENV === "production" ? "/" : "/",
};

remember, It's better if you use "@" to define your paths. for example, if you want to load a image which located in src/assets/files/img/myImage.png, you can use @/assets/files/img/myImage.png in template section for binding or script section of your .vue files! It always help you to find correct path of your files.

and finally your way is not standard because "src/assets/..." will used for compiled scripts and styles and also your files which you want to use on your UI layout like static images. so you have to use "public/assets/..." directory to save your file, then you will see everything is going well for you.

if you have a more question or stuck solving this problem again, I'm here to fix your issues.

Related