1

So I'm building my first vue app but I cant manage to find a way to import external CSS files under /assets/css. Is there a way to import external css files with vue? Do I need an extra package for this?

What I tried:

In main.js I tried adding import '@/assets/css/menu.css'; but it returns the following error when building the app:

 error  in ./src/assets/css/menu.css

Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleNotFoundError: Module not found: Error: Can't resolve '../images/menu-back.svg' in 'C:\Users\amng8\Desktop\Ruby\app\client\src\assets\css'
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\webpack\lib\Compilation.js:925:10
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\webpack\lib\NormalModuleFactory.js:401:22
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\webpack\lib\NormalModuleFactory.js:130:21
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\webpack\lib\NormalModuleFactory.js:224:22
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\neo-async\async.js:2830:7
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\neo-async\async.js:6877:13
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\webpack\lib\NormalModuleFactory.js:214:25
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\enhanced-resolve\lib\Resolver.js:213:14
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\enhanced-resolve\lib\Resolver.js:285:5
    at eval (eval at create (C:\Users\amng8\Desktop\Ruby\app\client\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\enhanced-resolve\lib\UnsafeCachePlugin.js:44:7
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\enhanced-resolve\lib\Resolver.js:285:5
    at eval (eval at create (C:\Users\amng8\Desktop\Ruby\app\client\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:15:1)
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\enhanced-resolve\lib\Resolver.js:285:5
    at eval (eval at create (C:\Users\amng8\Desktop\Ruby\app\client\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:27:1)
    at C:\Users\amng8\Desktop\Ruby\app\client\node_modules\enhanced-resolve\lib\DescriptionFilePlugin.js:67:43

 @ ./src/pages/index/main.js 7:0-31
 @ multi ./src/pages/index/main.js

Also tried to include

<link href="assets/css/common.css" rel="stylesheet" />
<link href="assets/css/menu.css" rel="stylesheet" />
<link href="assets/css/baseunit.css" rel="stylesheet" />

in the index.html under /public but apparently it can only get files from /public and I don't know if I should leave the files in the /public directory.

File directory print:

enter image description here

PS: I'm using a multi-page setup in this vue app.

I think that's all I tested. I'm new to vue.js so if you need any extra info let me know.

2
  • where is the menu-back.svg? Commented Jul 4, 2020 at 22:32
  • Didnt add it yet. Is that the problem? Commented Jul 4, 2020 at 22:52

1 Answer 1

1

Try in this way:

import './assets/css/menu.css'

Oh! I see you have your main.js in src/pages/index/main.js For this reason is not working.. Your main.js must be in src/main.js

If you want to keep your main.js in that path (src/pages/index/main.js), you need to change to

import '../../assets/css/menu.css'
Sign up to request clarification or add additional context in comments.

4 Comments

I suppose I should add that in the script tag but gives me this error: This relative module was not found: * ./assets/css/menu.css in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/thread-loader/dist/cjs.js!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/navbar.vue?vue&type=script&lang=js&
I tested in my vue app before answer and is working. I add in main.js
Oh! I see you have your main.js in src/pages/index/main.js For this reason is not working.. Your main.js must be in src/main.js I have edited the answer
Worked! How about js I tried doing the same thing but it gives a lot of errors. Do you know a way to import js files too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.