Create-React-App build - “Uncaught SyntaxError: Unexpected token <”

后端 未结 9 1459
予麋鹿
予麋鹿 2020-12-10 10:35

I realize this question has been asked multiple times but nothing has worked for me...

I\'m trying to create a static build of a create-react-app projec

9条回答
  •  余生分开走
    2020-12-10 10:54

    Ran into the same issue when I want to deploy the static build of a create-react-app project to my linux server.

    I solved with this issue comment on cra's github and the cra's official document about how to deploy production build.

    For example:

    I want to put the production build website under something like http://example.com/foo/bar.

    When I deploy without changing any default settings, I will get this "Uncaught SyntaxError: Unexpected token <" error and nothing shows up on the screen.

    Here is the solution:

    1) Add homepage parameter to your package.json.

    + "homepage": "/foo/bar"
    

    2) Add "/foo/bar" to all of your static resources in css which will be like:

    .dummyimage {
     - content: url('/dummyimage.jpg');
     + content: url('/foo/bar/dummyimage.jpg');
    }
    

    3) Add "/foo/bar" to all of your links and routes.

    - linkTo: '/contact'
    + linkTo: '/foo/bar/contact'
    

    4) Changing a little of your serve program's code, in node.js/express it will be like:

    - app.use(express.static(path.join(__dirname, '/build')));
    + app.use('/foo/bar/', express.static(path.join(__dirname, '/build')));
    

    5) Build again.

    6) Deploy build/ to the server.

    7) Restart your serve program, like node.js/express.

    Problem solved!!

    Hope this solution is helpful.

提交回复
热议问题