db2791 db2791 - 2 months ago 6x
AngularJS Question

External javascript in html is sent incorrectly to the server

I'm running a Node.js server along with an Angular frontend. One of the Angular dependencies I'm using requires me to import a javascript file into my html page, by the name of

. However, when I try to do this, it sends the required file as an http request to the server, resulting in requests that look like the following:


Obviously, this comes up as a 404. As an alternative, I've tried changing

<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>


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

and then on the server-side, doing:

app.get('swing.js', function(req, res){

This works a little more, but then the file doesn't run properly, as I'm assuming it's no longer in the npm environment it needs to be in. I've tried multiple iterations of changing

<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>

into something that uses periods (.) to represent more relative paths, but that doesn't appear to work either. Overall, I'm very stuck, and would appreciate any insight. If it's of any use, I'm also using:



Making my comments into an answer...

node.js does not serve any files by default so any script files that you need sent from your server to the client upon request need an explicit route to handle them or they need some generic route that knows how to handle all the requested script files.

swing.js in the client is not part of any "NPM environment". It's running the browser at that point, not in any NPM enviornment. It's possible that swing.js itself needs some other scripts or resources that also need routes and that's why it doesn't work after you make an explicit route for it. You can probably examine the specific errors in the console to give you a clue why it isn't working.

You may also want to read this: How to include scripts located inside the node_modules folder?