Continued from: Netlify functions failing to bundle when deployed to netlify
Just need someone to help me understand this warning.
I have did as instructed, but this breaks, when I do a call to the netlify function, even the build will be pushed successfully. So, I just removed the “external_node_module”, but am still worried about the errors that it may cause.
I followed the following resources which allowed me to get this far
and here is the link to my github repository: GitHub - Void074/vodafone-api, which I am using to deploy to netliy, this personal project all for the purpose of learning about netlify functions and how to use them
I cloned the project repository and deployed. I am able to access the /api/
and /api/users
endpoints without issue.
1 Like
This works because I have removed “external_node_modules” configuration out of the netlify.toml and should work as expected, but when I add in "external_node_modules=“express” to the netlify.toml, it breaks with a request to /api/ or /api/users is made.
the error I receive is “unable to to load module express”.
I don’t see any reason why you need to specify external_node_modules
. It is evident express
is bundled with the function.
that true, so I completely dropped the external_node_modules for the netlify.toml and will be sticking to that, was just look for validation and possible links to further resoureces about the external_node_nodules options the current documentation here: File-based configuration | Netlify Docs, doesn’t provides much.
on a side note, am using esbuild as my preferred “node_bundler” for bundling the functions had some issue with using @netlify/zip-it-and-ship-it as the blunder (but that requires more testing, as I believe it has something to do with versions of node, pnpm and packages that I am using to build my netlify functions)
When there are dynamic imports in node_modules
esbuild cannot follow the import chain and does not bundle these dependencies. external_node_modules
then tells esbuild that these dependencies are external, so esbuild leaves the import/require statement to the module as is, and during bundling we simply include the whole package ourselves (pretty much the same as included_files: ['node_modules/packagename/**']
)
1 Like
if I understand corrently, external_node_models
simple tells esbuild to ignore them as they are external. To include them we must add the included_files: ['node_modules/packagename/**]
where **
is a glob pattern and this line included_file
tells the netlify build bot to bundle them up.
In normal conditions, the code you import using import
or require
statements gets bundled in a single file and those lines are removed. But if a Node Module is imported dynamically, bundling that is currently not supported. external_node_modules
will ask esbuild to not change those import
or require
statements and the module is simply added to the bundle to be imported during runtime.
The included_files
will just produce a similar effect of simply including that files, you can use either of those two features. included_files
is technically meant to bundle other files like PDFs, images, etc. that cannot be directly imported in your JavaScript bundle but are required by your Function. It can do that as well as include the files from node_modules
.
2 Likes
thanks, after re-reading your responses its finally sinking. both do the same thing but with subtle differences. I will stick to content you provided
-
included_files
- mainly for files that my function my require (but can also be used in to include node_module)
-
external_node_module
- do not change the packages that being imported or require just bundle them up.