Sub topic 6
Bundling dependencies so the action runs
This chapter assumes you can write a JavaScript action with an action.yml file, and that you know npm basics. It covers why a JavaScript action must ship its dependencies, and how to bundle them into one file.
Why dependencies are a problem
When a workflow uses your action, the runner downloads your repository at the referenced tag or commit. It then runs the file named in main. The runner does not run npm install. If index.js calls require('@actions/core') and no node_modules folder exists in the checkout, the step fails.
The usual error looks like this:
Error: Cannot find module '@actions/core'You have two ways to avoid it:
| Approach | What you commit | Trade-off |
|---|---|---|
| Commit `node_modules` | The whole dependency tree | Large, noisy diffs and slow clones |
| Bundle into one file | A single generated file, usually `dist/index.js` | Needs a build step, and you must rebuild before each release |
Bundling is the common choice. A bundler reads your entry file, follows every require, and writes one file that contains your code and its dependencies.
Bundling with @vercel/ncc
@vercel/ncc is a bundler built for this job. Install it as a development dependency, along with the toolkit packages your code uses.
npm install @actions/core @actions/github
npm install --save-dev @vercel/nccAdd a build script to package.json:
{
"name": "hello-action",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "ncc build index.js -o dist"
},
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0"
},
"devDependencies": {
"@vercel/ncc": "^0.38.1"
}
}Run the build:
npm run buildThis writes dist/index.js. Now point action.yml at the bundle instead of the source file:
runs:
using: 'node20'
main: 'dist/index.js'Commit dist/index.js to the repository. Do not add dist to .gitignore, because the runner needs that file in the checkout.
What the build does
Only dist/index.js and action.yml matter at run time. node_modules stays out of version control.
Common errors
- Stale bundle. You edit
index.jsbut forget to run the build. The runner executes the olddist/index.js, so your change appears to have no effect. Rebuild and commitdistwith every source change. mainpoints at the wrong file. Ifaction.ymlstill saysindex.js, the bundle is never used and the missing-module error returns.distignored by Git. A global or project.gitignoreentry fordistkeeps the bundle out of the repository. Check withgit statusthatdist/index.jsis tracked.- Runtime mismatch. The bundle targets the Node version you build with. Keep your local Node version close to the
using:value, such asnode20.
Checking the bundle locally
After a build, confirm that the bundle runs without node_modules. Action inputs reach the script as INPUT_ environment variables, with the name upper-cased.
mv node_modules node_modules.bak
INPUT_WHO-TO-GREET=Mona node dist/index.js
mv node_modules.bak node_modulesSome shells reject environment variable names that contain a hyphen. If yours does, use env 'INPUT_WHO-TO-GREET=Mona' node dist/index.js instead. The script prints Hello Mona! if the bundle contains everything it needs.
Exercise
Your action works on your machine, but in a workflow the step fails with "Cannot find module '@actions/core'". action.yml has main: 'index.js' and node_modules is in .gitignore. What are two changes that fix it, and which is preferred?
Show answer
Either commit node_modules, or bundle with ncc build index.js -o dist, set main: 'dist/index.js', and commit dist/index.js. Bundling is preferred because it commits one generated file instead of the whole dependency tree.
Exercise
You change the greeting text in index.js, push, and the workflow still prints the old text. What is the most likely cause?
Show answer
You did not rebuild and commit dist/index.js. The runner executes the committed bundle, not your source file. Run npm run build and commit the updated dist folder.