Node 18: fix missing rollup-linux-x64-gnu package
In case you code a node18
app locally on a M1 machine (ARM64
arch) but you deploy or run tests on a X64
machine (e.g. gitlab CI and AWS lamba runtime), you might get an error related to a missing package on the different architecture.
In my case I got an error relate to a missing @rollup/rollup-linux-x64-gnu
package.
The solution was installing with --optional=true
so that that package is also installed on a different architecture, I could also potentially deploy from local.
To enforce this, I created a .npmrc
file with this content
include=optional
And also added a test to make sure on the CI I’m runing the same version
describe('node env', () => {
it('node version should be the same as AWS environment', async () => {
expect(process.version).toBe("v18.20.4");
expect(process.release.lts).toBe("Hydrogen");
});
});
In case you need to debug on M1 node on a different architecture, you can use this docker command to replicate the error on gitlab pipeline for example, so you can test your fix
docker run --platform linux/amd64 -it -v `pwd`:/temp node:18 sh -c "cd /temp; npm run test"
# replace last part with the failing command
Other useful info: M1 brew with node@18
has exactly the same node version as AWS lambda and docker image node:18
used in gitlab CI. Good to know the run on the same versions of node an npm. The only thing to make sure is that optional packages are installed.
An alternative approach to solve this problem is running local, CI and AWS lambda on ARM64. I read the AWS lambda supports that architecture, but I could not find the way to run gitlab ARM64 image, so I had to abandon the idea.
More on the optional package here https://github.com/npm/cli/issues/4828