# Run 'npm ci' and 'npm run build' only once

**URL:** https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902
**Category:** Node.js
**Created:** [December 23, 2020, 10:09am UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902 "2020-12-23T10:09:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![daraclare](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/daraclare/32/5905_2.png) [@daraclare](https://travis-ci.community/u/daraclare)
#### Post date: [December 23, 2020, 10:09am UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902/1 "2020-12-23T10:09:45Z")

</div>

Hi, I’ve split my tests up so they run in different stages, however, using the config below, ‘npm ci’ and ‘npm run build’ are run before each test. Is it possible to run these scripts only once? My aim is to speed up the pipeline by running our unit and integration tests separately, so if one fails, it can be run independently without running the other. Our cypress integration tests are particularly slow, and can take up to 30 minutes. Any suggestions?

```auto
language: node_js

node_js:
  - 12

addons:
  apt:
    packages:
      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
      - libgconf-2-4

before_install:
  - npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

cache:
  directories:
    - ~/.npm
    - ~/.cache
  override:
    - npm ci
    - npm run build

defaults: &defaults
  script:
    - npm run $STAGE_NAME

jobs:
  include:
    - stage: test
      env:
        - STAGE_NAME="test"
      <<: *defaults
    - stage: test
      env:
        - STAGE_NAME="integration"
      <<: *defaults

```

---

<div class="post-metadata">

### Author: ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)
#### Post date: [December 23, 2020, 2:37pm UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902/2 "2020-12-23T14:37:36Z")

</div>

If you wish to skip creating build artifacts in succeeding stages and rather use ones created by a preceding stage, you can transfer them using workspaces:

> **[Travis CI Documentation](https://docs.travis-ci.com/user/using-workspaces)**
>
> Using Workspaces (Beta)

---

<div class="post-metadata">

### Author: ![daraclare](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/daraclare/32/5905_2.png) [@daraclare](https://travis-ci.community/u/daraclare)
#### Post date: [January 4, 2021, 2:04pm UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902/3 "2021-01-04T14:04:39Z")

</div>

Thanks for the advice, much appreciated. I have changed my config file to be:

```auto
language: node_js

node_js:
  - 12

addons:
  apt:
    packages:
      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
      - libgconf-2-4

before_install:
  - npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

cache:
  directories:
    - ~/.npm
    - ~/.cache

defaults: &defaults
  script:
    - npm run $STAGE_NAME

jobs:
  include:
    - stage: build
      script:
        - npm ci
        - npm run build
      workspaces:
        create:
          name: build1
    - stage: test
      workspaces:
        use: build1
      env:
        - STAGE_NAME="test"
      <<: *defaults
    - stage: integration
      workspaces:
        use: build1
      env:
        - STAGE_NAME="integration"
      <<: *defaults

```

but the stage called “integration” still runs `npm run build` before it starts running its own script. Could you advise me how I can speed this up at all? Any suggestions would be very welcome! Thanks again.

---

<div class="post-metadata">

### Author: ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)
#### Post date: [January 4, 2021, 2:41pm UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902/4 "2021-01-04T14:41:22Z")

</div>

Could you link to the build? Can’t quite say what’s happening without seeing it.

My best blind guess is the extra command is [the default `install` command for `language: node_js`](https://docs.travis-ci.com/user/languages/javascript-with-nodejs/). To skip it, specify `install: skip` in the specific job(s).

---

<div class="post-metadata">

### Author: ![daraclare](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/daraclare/32/5905_2.png) [@daraclare](https://travis-ci.community/u/daraclare)
#### Post date: [January 4, 2021, 2:57pm UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902/5 "2021-01-04T14:57:47Z")

</div>

Ah, I think I have it, there was a build command in the package.json, so it wasn’t coming from the travis.yml file, apologies for the mixup. Could you recommend anyway of making this faster, do I have the correct files cached etc? Thanks for any advice!

---

<div class="post-metadata">

### Author: ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)
#### Post date: [January 4, 2021, 9:53pm UTC](https://travis-ci.community/t/run-npm-ci-and-npm-run-build-only-once/10902/6 "2021-01-04T21:53:06Z")

</div>

I don’t know much about NPM and you didn’t show me the build for me to see what’s happening.

So all I can say is I see your workspace is currently empty. You probably want to add your build artifacts and/or NPM cache locations to it – depending on what your later stages need to be present.
