Home > Mobile >  How can I set the npm version in gitlab CI?
How can I set the npm version in gitlab CI?

Time:09-22

I have a gitlab pipeline for an Angular project with the image in .gitlab-ci.yml

image: node:16


build:
  stage: build
  script:
    - npm ci
    - nodejs -v
    - npm -v
    - npm run build:prod
    - npm doctor

When the pipeline reaches the npm doctor it fails with the following error on npm version:

$ npm doctor
Check                               Value   Recommendation/Notes
npm ping                            ok
npm -v                              not ok  Use npm v7.24.0
node -v                             ok      current: v16.9.1, recommended: v16.9.1
npm config get registry             ok      using default registry (https://registry.npmjs.org/)
which git                           ok      /usr/bin/git
Perms check on cached files         ok
Perms check on local node_modules   ok
Perms check on global node_modules  ok
Perms check on local bin folder     ok
Perms check on global bin folder    ok
Verify cache contents               ok      verified 1361 tarballs

How can I solve this?

In my machine I have the npm version v7.24.0 and the command succeeds.

CodePudding user response:

Install npm before you do anything else, specifying a version, i.e. npm install npm@version -g.

For completeness, if you can't update the global npm, you can do so locally, by replacing npm with npx npm@latest or npx [email protected]. npx will install npm if it needs to, locally, and then run the local installation.

Lastly you can install locally with npm install npm@latest and then run it with $(npm bin)/npm, but this is what npx is for.

Note that I don't understand why you're running npm doctor after a build. Presumably if you care about npm thining it's set up properly you should run it before the build, so the pipeline fails early.

P.S. I stupidly read 'github' as 'gitlab'. If you were using github I would recommend using setup-node if only for dependency caching, which can seriously speed up pipelines. I don't know if gitlab does anything similar.

  • Related