How do I include external javascript file?


I’m brand new to Netlify. Been digging through the docs an have learned how to get a basic site up with GutHub ( As I begin to make the leap from full stack to serverless (bit of a challenge in thinking for me I’m finding), I’m not understanding how to include external javascript files like I’ve done previously:

<script src="js/file.js"></script>
<script src="js/file2.js"></script>

Specifically, I need to run some javascript on the server side (?) as it contains api keys (already set up in the Environment section of Netlify), so they will not be public. Sorry if this is vague, but I’m not entirely sure how to ask in the clearest Netlify vernacular. Any help would be appreciated.


hi there, i suggest that you could try downloading the site that we end up building, see this post:

and then you’ll have a bit more insight into the directory structure. It is totally possible to load in whatever you like, as long as the paths are correct.

Thanks for the suggestion. But as I’m dealing with Functions, that’s code that is not visible in the site’s code, so downloading the site would not help in that regard. Is that correct? Thanks.

If you mean to include JS files in functions, I don’t think that’s possible. You’d have to use Node Modules and compile the functions accordingly.

Ah - got it. I’m very new to Node (NodeJS?). Node(JS?) is pretty new for me. I’m presuming there are Node Modules that are “pre-loaded”, that can be “require()”-ed, such as “crypto”, perhaps? And if you want a module that is not pre-loaded, you have to “install it”? Thanks.

I’m not sure what you mean by pre-loaded. I’ve not used Node Modules with Functions myself, or heavily used Functions for that matter, so that area is still a bit in grey for me. But, I’ll tell you what I know:

You need to add a package.json in the root of your repo with the dependencies that you need. Netlify will install them during build. You then need to have your Functions like import foo from bar where foo would be variable name and bar would be module name. Then you can use it in your Functions code. When Netlify starts the build, it’ll install the dependencies, compile the Functions code (perhaps) and it would work.

Hi, @tddesign. These two support guides sound like they could be helpful as well:

If there are other questions about Functions at Netlify, please feel free to post again here or to make a new topic anytime.

Hi All,

Thanks very much for all your help. I’m moving forward - starting to learn node now.

1 Like