vue cli - Uncaught SyntaxError: Unexpected token <

前端 未结 14 2164
说谎
说谎 2020-12-31 03:39

I create my project with vue-cli 3.0. Initially it runs ok. But after I -c then npm run serve again, it keep throwing erro

相关标签:
14条回答
  • 2020-12-31 03:56

    Try by adding <base href="/" /> into the <head> of your index.html. Hope it will work.

    0 讨论(0)
  • 2020-12-31 04:00

    I have the same problem. in index.html file , I just write this script tag,like this :

      <script src="./static/js/qrcode.min.js"></script>
    

    if I change src attr like this:

     <script src="/static/js/qrcode.min.js"></script>
    

    ok, no error 'Unexpected token <' dispaly.

    here is a another problem happened,it is upsetting to me .

    online build product tests that dispaly I can't access this site and get resource. OMG ! ok, I change this src again like this:

    <script src="./static/js/qrcode.min.js"></script>
    

    and then make some changes in vue.config.js like this:

    baseUrl:'./'
    assetsDir:'./'
    

    so,online everything is ok ,but in my local project run it will get this error.

    finally,I find this :

    online url address: http://xxxxx.cn/test0001/#/

    local url address: http://192.168.5.108:9000/test0001/#/

    (ps:test0001 params can't remove,my project need it)

    if I remove this params 'test0001', this error isn't display in my local project test .

    I guess build and development environment's difference cause this error,and my params test0001 in url address.

    finally,I just use a param to diff this product and development to solve it.

    if someone meet this problem like me , please share your solution.

    0 讨论(0)
  • 2020-12-31 04:01

    I had the same problem. For me I solve it by adding the 'asset'

    <script src="{{ asset('js/app.js') }}" defer></script>
    
    0 讨论(0)
  • 2020-12-31 04:02

    Uninstalling both CLI's and reinstalling V3 + A 'hard' refresh in Chrome did the trick. Maybe a hard refresh alone was enough (CTRL + F5)

    0 讨论(0)
  • 2020-12-31 04:06

    I had the same problem, I tried the removing node modules and npm install again but that didn't work.

    What did work was removing all the site data. You can do that (in Chrome) by opening the development tab (Ctrl+Shift+i), go to the 'application' tab, click "Clear storage" on the side panel and click on the "Clear site data" button.

    My guess is that I still had some old service-worker that intercepted the request and served the html file instead of the app.js. So if you have used PWA's before at the url "http://localhost:8080" this might solve the problem.

    0 讨论(0)
  • 2020-12-31 04:06
    // vue.config.js
    module.exports = {
      publicPath: '/',
    };
    

    You actually want to add the publicPath to your vue.config.js file

    https://stackoverflow.com/a/56320964/1321009 does work if you application is deployed at http://example.com

    If you deployed it at http://example.com/something

    Then you would need to go in and change it when deploying, annoying to say the least.

    with vue.config.js file just set it like this

    // vue.config.js
    module.exports = {
      publicPath: process.env.NODE_ENV === 'production'
        ? '/something/'
        : '/'
    };
    
    0 讨论(0)
提交回复
热议问题