Syntax Error in Angular App: Unexpected token <

后端 未结 30 1925
独厮守ぢ
独厮守ぢ 2020-12-02 20:16

I have an Angular app which runs perfectly in my local and production environment.. After a tiny change I made, I ran the app locally and it works fine.. Then I built the pr

相关标签:
30条回答
  • 2020-12-02 20:18

    In my case was a mismatch route path in the server file. the route to statics must be the dir containing both the index.html and the assets dir, not the assets dir itself.

    app.use(express.static(path.resolve(__dirname,'/dist')));
    
    app.get('/*', function (req, res) {
      res.sendFile(path.join(path.resolve(__dirname, '/dist/index.html')));
    });
    
    0 讨论(0)
  • 2020-12-02 20:20

    This is most likely the result of a 404 page or a redirect to a page that serves regular html instead of the expected JavaScript files. (A HTML page starts with <html> or a <!DOCTYPE...>)

    Make sure that you have correctly uploaded the files and access the page correctly. You can verify by manually accessing the URL with the browser or look into the network tab of your browser development tools to inspect the response.

    0 讨论(0)
  • 2020-12-02 20:20

    For a quick workaround though (WHICH IS DEFINITELY NOT RECOMMENDED AND NOT A GOOD PRACTICE) you can build in non prod mode. Try ng build only.

    0 讨论(0)
  • 2020-12-02 20:20

    I also had the same problem a couple of times, I solved it this way:

    Build the project with a user other than root (ubuntu - normaluser)

    root$ chown -R ubuntu:ubuntu my-project-folder/
    ubuntu$ cd my-project-folder/
    ubuntu$ npm install 
    ubuntu$ ng build -xxx params xx etc
    

    In production server show nginx user :

    root$ grep -rh user /etc/nginx/*
    user www-data; # in my case
    

    Now move or replace my-project-folder

    root$ mv my-project-folder/ /var/www/awesome.com
    

    And change permissions to nginx user

    root$ chown -R www-data:www-data /var/www/awesome.com
    

    (Y)

    0 讨论(0)
  • 2020-12-02 20:20

    in your index.ts change

    <!--doctype html-->
    <!DOCTYPE html>
    

    remember... The <!DOCTYPE> declaration is NOT case sensitive. check the following link

    0 讨论(0)
  • 2020-12-02 20:22

    The problem is with the <base href="/"> part of index.html file, it seems angular generates build file inside dist/{yourProjectName}/ but the index.html files goes through the dist/ for build files. Now you have 2 options:

    1. Change the <base href="/"> part of the index.html file to <base href="/dist/{yourProjectName}/"> but now you have inconsistency between the ng serve command and ng build and you can't see your project through ng serve. So you have change that part every time!

    2. The seccond approach that I recommend, is just changing the output path of the project! Go to your angular.json file of your project and change the "outputPath": "dist/{yourProjcetName}", to "outputPath": "dist/" and don't change the base href!

    0 讨论(0)
提交回复
热议问题