Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html image src require not respecting webpack aliases? #193

Closed
alexbakerdev opened this issue Mar 30, 2016 · 30 comments
Closed

html image src require not respecting webpack aliases? #193

alexbakerdev opened this issue Mar 30, 2016 · 30 comments

Comments

@alexbakerdev
Copy link

alexbakerdev commented Mar 30, 2016

Hey there,
I'm new to vue (which I love so far), just setting up an example project.
Hoping someone can tell me where I'm going wrong with aliasing requires.

Using the vue-cli webpack template for example, changing App.vue template to

<template>
  <div id="app">
    <img class="logo" src="assets/logo.png">


...

<script>
  import Hello from 'components/Hello'

and webpack.base.conf.js

  alias: {
    'src': path.resolve(__dirname, '../src'),
    'assets': path.resolve(__dirname, '../src/assets'),
    'components': path.resolve(__dirname, '../src/components')
  }

Then moving App.vue into the folder src/App/ as index.vue webpack.
npm run build
gives:

ERROR in ./~/vue-html-loader!./~/vue-loader/lib/selector.js?type=template&index=0!./src/App/index.vue
Module not found: Error: Cannot resolve 'file' or 'directory' ./assets/logo.png in /Users/Alex/Work/testing/quick/src/App
 @ ./~/vue-html-loader!./~/vue-loader/lib/selector.js?type=template&index=0!./src/App/index.vue 1:55-83

Not sure what is going on here the image seems to be taken care of by url-loader as its encoded into the js. Which I think differentiates it from this issue #172 which Evan already replied to.

@alexbakerdev alexbakerdev changed the title html image src not require not respecting webpack aliases? html image src require not respecting webpack aliases? Mar 30, 2016
@yyx990803
Copy link
Member

yyx990803 commented Apr 6, 2016

vue-html-loader and css-loader translates non-root URLs to relative paths. In order to treat it like a module path, prefix it with ~:

<img class="logo" src="~assets/logo.png">

@k1sul1
Copy link

k1sul1 commented Aug 29, 2016

Not directly related, but if you're using the vue-cli webpack boilerplate and webpack is giving you pain when trying to bind images from the default data object (or whatever this is called, my first Vue project)..:

export default {
  data() {
    return {
      // note: changing this line won't causes changes
      // with hot-reload because the reloaded component
      // preserves its current state and we are modifying
      // its initial state.
      msg: 'Hello World!',
      img: '../assets/image.png'
    };
  },
};

<img :src="img"> will result in you scratching your head and wondering what you did wrong. Webpack "has no means of rewriting it" and it doesn't work. Wrap it in require() and if Airbnb ESLint gives you trouble, turn global-require to 0 in .eslintrc.js.

vuejs-templates/webpack#126

@PrimozRome
Copy link

PrimozRome commented Sep 27, 2016

@k1sul1 I tried this solution but I am getting error Uncaught Error: Cannot find module '../assets/img/profiles/6.jpg'. Any idea?

@k1sul1
Copy link

k1sul1 commented Sep 27, 2016

Well, if you actually read the error message, you might actually figure it out :)

It clearly says "Cannot find module", so your path is probably broken. Meaning the file doesn't exist in that location :)

@PrimozRome
Copy link

PrimozRome commented Sep 27, 2016

@k1sul1 ok please don't think I am posting this before checking something like that... Ofcourse it exists... For example this is working:

<img src="../assets/img/profiles/6.jpg">

while this is not with the error I posted above

<img :src="loadImg('../assets/img/profiles/6.jpg')">

loadImg: function (path) {
   require(path)
}

@k1sul1
Copy link

k1sul1 commented Sep 27, 2016

Interesting. Can't say I have any ideas other than adding return to the function, but I'm assuming you have that in your actual code. I'm not an expert in any of these things so I can't really help you.

Could be a platform issue, are you using Windows perhaps?

For me, require just worked straight away.

@PrimozRome
Copy link

PrimozRome commented Sep 27, 2016

@k1sul1 tried that too when I noticed I don't have one, but without any luck. We are discussing this on the Vue.js forum as well but no success right now http://forum.vuejs.org/topic/5343/webpack-serve-dynamic-images/15.

@ghost
Copy link

ghost commented Feb 6, 2017

thx~

@oswaldofreitas
Copy link

oswaldofreitas commented Apr 17, 2017

this code...

<template lang="pug">
  section
    img(src='~assets/logo.png')
</template>

is returning:

This dependency was not found:

  • assets/logo.png in ./~ /vue-loader/lib/template-compiler?{"id":"data-v-29b5cd26"}!./~ /v
    ue-loader/lib/template-compiler/preprocessor.js?raw&engine=pug!./~/vue-loader/lib/select
    or.js?type=template&index=0!./src/components/layout/Navbar.vue

screen shot 2017-04-17 at 11 52 36

@oswaldofreitas
Copy link

oswaldofreitas commented Apr 17, 2017

no more, changed to img.logo(src='~@/assets/logo.png') and worked

@codeofsumit
Copy link

codeofsumit commented May 3, 2017

thanks @oswaldomilet

<img src="~@/assets/logo.png" />

worked for me. Very weird...

shinja added a commit to shinja/vue-bootstrap-scss that referenced this issue May 10, 2017
yokotak0527 pushed a commit to yokotak0527/webpack-spa-env that referenced this issue May 16, 2017
@imsunf
Copy link

imsunf commented Jul 5, 2017

my solution!

1.<img :src="icon_src" />
2.import the image at the top level
import icon from 'icon.png';
3. data () { return { icon_src:icon } }
or computed instead

@tangdaohai
Copy link

tangdaohai commented Jul 14, 2017

I have another a way:

<img :src="require('@/assets/logo.png')" />

@jofftiquez
Copy link

jofftiquez commented Jul 19, 2017

Not all heroes wear capes. Thanks @oswaldofreitas The future generations will remember you.

@oswaldofreitas
Copy link

oswaldofreitas commented Jul 19, 2017

hahaha, funny. Thanks @jofftiquez

@paladin2005
Copy link

paladin2005 commented Jul 19, 2017

In my case it works with url('~@/assets/.......png')

@jofftiquez
Copy link

jofftiquez commented Jul 19, 2017

@paladin2005 you mean it WORKS or DOES NOT?

@paladin2005
Copy link

paladin2005 commented Jul 19, 2017

@jofftiquez I already edited y last comment. Sorry my english.

@AburronKing
Copy link

AburronKing commented Jul 21, 2017

In data(){return{require()}},how to writh this path

@AburronKing
Copy link

AburronKing commented Jul 21, 2017

write this

@AburronKing
Copy link

AburronKing commented Jul 21, 2017

Or in css url(~) ?

@jofftiquez
Copy link

jofftiquez commented Jul 21, 2017

<template>
<div>
    <img :src="logo">
</div>
</template>

<script>
import image from '@/assets/logo.png';

export default {
    data() {
        logo: image;
    }
}
</script>

This @/ also works.

@bcowell
Copy link

bcowell commented Jul 21, 2017

<template>
  <div>
    <md-boards>
      <md-board v-for="image in images" :key="image.header">
         <img :src="image.src">{{ image.header }}</img>
      </md-board>
    </md-boards>
  </div>
</template>

<script>
import image from '@/assets/img1.jpg'
export default {
  name: 'Banner',
  data: function () {
    return {
      images: [
        { src: image, header: 'img1' },
        { src: image, header: 'img2' },
        { src: image, header: 'img3' }
      ]
    }
  }
}
</script>

@jofftiquez weird, I'm trying the same thing and getting an error.

ERROR Failed to compile with 1 errors
This dependency was not found:

@proYang
Copy link

proYang commented Aug 11, 2017

<img class="logo" src="~assets/logo.png">

why use "~", it will work. I want to kown the reason

@oswaldofreitas
Copy link

oswaldofreitas commented Aug 11, 2017

@proYang Please see the docs:

URLs prefixed with ~ are treated as a module request

@helmerdavila
Copy link

helmerdavila commented Apr 27, 2018

@oswaldofreitas you are the man 👍

@Sangeethks
Copy link

Sangeethks commented May 9, 2018

If you are using data property to assign image to src, then use require(<image_path>). Like,

<img :src="image">

data() { return { image: require('@/assets/icons/profile.png') } }

@jofftiquez
Copy link

jofftiquez commented Oct 25, 2018

Another solution that I am using is <img :src="require('image/path/here')">

@motivis
Copy link

motivis commented Nov 29, 2018

does anyone know how to npm install the package in order to use REQUIRE in the client application ?

I forgot what package that is

@brianwachira
Copy link

brianwachira commented Jan 28, 2019

thanks @oswaldomilet

<img src="~@/assets/logo.png" />

worked for me. Very weird...

worked for me too.
src="~@/assets/image.png"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests