0

I'm trying to include some scripts to my vue app. but all that i've tried so far didn't work.

I'm using Vue 2 and created my project with the vue CLI.

when i try just adding the script tags to my index.html

<script src="../src/assets/js/typewriter-effect.min.js"></script> 
<script src="../src/assets/js/custom.js"></script> 

I get this error:

Uncaught SyntaxError: Unexpected token '<'

Which i later understood from a post:

So if you are running the local dev server for instance, the browser tries to load the file being served at http://localhost:8080/~some-npm-package/path/to/lib.js.

The dev server is setup to just serve whatever is in the index.html regardless of the actual URL, so it serves the index.html when you try and fetch http://localhost:8080/~some-npm-package/path/to/lib.js.

The browser tries to read this file as a script but the first line of the index.html file is - hence the SyntaxError: expected expression, got '<' that you see.

So my workaround was to use CDN, but even though I could see form devTools that the script was loaded, it didnt work on the page, there was no change. Besides, some of my scripts are only available locally and not from CDNs.

I tried importing in main.js

import './assets/js/slick.min.js'
import './assets/js/custom.js'

or from App.vue

But that didnt seem to work.

8
  • See this SO question/answer Commented Apr 27, 2021 at 23:29
  • @Tim Tried that already. Commented Apr 27, 2021 at 23:37
  • when you say importing in main.js doesn't work, what does it happen, did it shows you an error? are you sure that you have your files on src/assets/js/ Commented Apr 27, 2021 at 23:37
  • @jc.vargas.valencia there is no error; but the expected behaviour, doesnt happen. for instance, i use a carousel library, but the carousel is not effective. Commented Apr 27, 2021 at 23:39
  • 2
    Did you check cli.vuejs.org/guide/… ? Put it to public. Load it as src="<%= BASE_URL %>...". End of the story. Commented Apr 28, 2021 at 0:52

0

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.