Why path in –  May 13 '19 at 04:14

3 Answers3

5

In this particular case the error is produced, because I have just opened page.html in a browser on my machine (i.e. I have not served it via server), thus / is interpreted as local machine's root. If I have served the project, then / would be interpreted as project's root, and there would be no error.


A little more elaborate explanation/illustration.
Given this structure:

/root
  /static
    script.js
  page.html

Here are what different paths (in page.html) will refer to:

  • / — root directory, it might mean /root, but also might mean the root of current environment (see explanation at the beginning of this answer)
  • ./ — current (the one where page.html resides) directory, it just so happens that it is /root in this case
    • it is worth mentioning that ./ can be omitted altogether, when referencing some file in current directory; so ./static/script.js is equivalent to static/script.js

I have derived understanding needed for this answer, from:

2

The error is there because there is no such file in your system's root directory.

This can easily be solved by serving your /root folder and accessing the page.html via that server.

You can find a list of static file servers Here.

Going with Node.js's http-server.

Open a terminal in your '/root' directory and run the following commands:

npm install -g http-server http-server -p 8000

Then you can access your page.html file at http://localhost:8000/page.html

Aayush Sharma
  • 779
  • 4
  • 20
0

If you have any directory into your root folder and your js file into that then you have to mention path only with folder name then file name like:

folder-name/filename.js 

because into web browser there is no need to add forward slash into starting of file path browser so it by self

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Ankit Soni
  • 19
  • 2