简体   繁体   中英

404 error while import js file from another folders in index.html in nodejs + Ionic app

I have error and am trying to solve it for hours. My file structure:

-/node_modules
-/www
---bundle.js
---index.html

in index.html I have such code

<script src="node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<script src="bundle.js"></script>

Problem is bundle.js is included fine, but ng-cordova.min.js gives 404 error

Cannot GET /node_modules/ng-cordova/dist/ng-cordova.min.js

Edit : I also tried this without success

<script src="/node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>
<script src="../node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

I don't use express or express.static .

Judging by your project structure I'm betting that it is powered by Express.js . I'm also betting that the www folder was set as a static folder in the express app like this:

app.use('/', express.static(path.join(__dirname, www)));

In which case your server will only serve files located in the www folder. You need to include any client dependencies in the www folder:

-/node_modules/
-/www/
--lib/
---ng-cordova/
--bundle.js
--index.html

And then load them like so:

<script src="lib/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

UPDATE

Simply put node_modules inside the www folder, it should work fine.

The problem here is that you try to load your ng-cordova.min.js file from the index.html folder. You should change:

<script src="node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

For this:

<script src="../node_modules/ng-cordova/dist/ng-cordova.min.js" type="text/javascript"></script>

You are trying to load the file from the wrong directory. You either have to use this path:

src="../node_modules/

or this:

src="/node_modules"

in case the above directory is your root directory.

What's also confusing is that the line you commented the error in is not the line the error actually comes from. Why are you trying to link to the file twice?

//Edit: As Romain was a minute faster than me he deserves the right answer reward I'd say :P Only if it actually solves your problem ofc :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM