1

I have added a local javascript file with <script type='text/javascript' src='Test.js'> in my index.html, but when I run it with ng serve and go to localhost:4200, in my browser console I see this error:

GET localhost:4200/Test.js 404 (Not Found) but I do have my file in the same location that my index.html.

but angular 2 not loading or finding this. Please help me.

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
Vidhya Sagar
  • 33
  • 1
  • 11
  • What error do you get... – Juan Jun 24 '17 at 20:29
  • GET http://localhost:4200/Test.js 404 (Not Found) I get this error. – Vidhya Sagar Jun 24 '17 at 20:30
  • 1
    Welcome to Stalk Overflow. Update that as part of your question before you get a wave of downvotes. See this: https://stackoverflow.com/help/how-to-ask. And when you get an answer that is acceptable to you, don't forget to mark it as so: https://stackoverflow.com/help/someone-answers. – Juan Jun 24 '17 at 20:40

1 Answers1

8

Move your Test.js file to the assets folder. Then from index.html, point to that folder <script type='text/javascript' src='./assets/Test.js' >

WHY? When you serve your app with ng serve, there's a structure set for it. If you want to see it, run ng build, you will see a dist folder being created.

Observe the assets folder gets move to that dist folder, and that's how the app expects it. So you are getting the not found error because from the server's perspective, there's no such file there.

When you make those changes and run ng serve, things should work and error should go away.

Hope this is of help.

Juan
  • 6,125
  • 3
  • 30
  • 31
  • If you could somehow decipher his question to a workable answer, would you be so kind to propose an edit to the question? – Stephan Bijzitter Jun 24 '17 at 20:45
  • Of course @StephanBijzitter. I didn't know I could do that so I had requested on a comment to the OP to update his question. – Juan Jun 24 '17 at 20:50
  • Submitted edit suggestion @StephanBijzitter. And you are welcome @VidhyaSagar! – Juan Jun 24 '17 at 21:09
  • @Juan But if i want to run the script file in a component html file. Is that possible ? Because now i don't get error and it doesnt recognize the script anymore. – Vidhya Sagar Jun 24 '17 at 22:14