Why i get Not found for JavaScript file

1.9k Views Asked by At

When i want to put js file i get this error

GET http://localhost:3000/src/app.js net::ERR_ABORTED 404 (Not Found)

HTML

 <!DOCTYPE html>
    <html>
      <head>
        <title>Pug with Java</title>
      </head>
      <body>
        <script src="src/app.js"></script>
      </body>
    </html>

Structure of the project

project structure

3

There are 3 best solutions below

7
On

You need to build your app. If you open package.json you will see scripts to build the app.

Build it and include the js from build folder. If your index.html and js file is in same location you dont need to src/ in script src.

Most probably app.js will be changed to main.js after build. Check it in build folder.

Build command will be something like

npm run prod

or

npm run prod-en

Once you do a build you will not have the src folder.

2
On

In which file is your HTML? If it's in index.pug you should try <script src="app.js"></script> as it's in the same folder.

0
On

if you use express.js ,try this , add this code app.use(express.static(path.join(__dirname, ""))); into your app.js to identify the exacly path. to your file.js