I’m brand new to Netlify. Been digging through the docs an have learned how to get a basic site up with GutHub (https://eager-johnson-677c66.netlify.app/). 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:
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.
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.