4

I am creating small angular application in which I have placed app.js in my application folder where index.html is also there. When, I run 'ng serve' command then index.html is loading but while doing F12 on chrome there it is showing up below error: GET http://localhost:4200/app.js net::ERR_ABORTED 404 (Not Found)

my index.html file:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>GridDemo</title>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src="app.js"></script>
</head>
<body>
  <app-root></app-root>
</body>
</html>

Even though adding <base href="/"></base> is not making any impact.

6
  • What is app.js is that your own javascript file? Commented Jun 13, 2019 at 17:33
  • Are you using angular-cli? Commented Jun 13, 2019 at 17:40
  • Yes, I am using angular-cli. Commented Jun 13, 2019 at 18:14
  • app.js is my file. Commented Jun 13, 2019 at 18:15
  • 1
    If you want to access directly a file that way, you need to put it in the assets folder Commented Jun 13, 2019 at 18:33

1 Answer 1

7

Being a bit more specific to what @David said, the js file needs to be included within a folder defined as an asset in the angular.json file. If you created the app with the cli, an "assets" folder should have been setup that way by default. So, I'd expect something like: <script src="assets/js/app.js"></script>

The exact property in the angular.json is projects.${app-name}.architect.build.options.assets and takes an array of strings.

But as to what @Harini P said, it is recommended to not do any dom changes outside of the regular angular file structure.

Sign up to request clarification or add additional context in comments.

1 Comment

I have made an entry of 'app.js' in angular.json like below: "assets": [ "src/favicon.ico", "src/assets", "src/app.js", "src/main.css" ] app.js is placed in 'src' folder only and it works for me. Thanks for the answer :)

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.