4

I'm sure this is a common issue but I can't seem to find a definitive answer.

I have a node application which in order to build requires some devDependencies such as babel. In order to run my tests also requires devDependencies such as jest. But when CI runs in production environment, it obviously doesn't install any devDependencies so I get errors where the package isn't found.

What is the best practice for running builds and tests in prod without devDependencies?

If it helps, I am running my build in GitLab Pipelines:

image: node:8.11.2

stages:
  - prepare
  - test
  - deploy

install_and_build:
  stage: prepare
  script:
    - npm install yarn
    - yarn
    - yarn build
  only:
    - master

test:
  stage: test
  script:
    - yarn test
  only:
    - master

deploy_production:
  type: deploy
  stage: deploy
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=app-name --api-key=$HEROKU_API_KEY
  only:
    - master
Stretch0
  • 8,362
  • 13
  • 71
  • 133
  • Why do you want to run your tests in production? – loganfsmyth Aug 17 '18 at 23:43
  • Because I don't have a staging branch / environment. Regardless of testing, I still need to run my build which requires babel dev dependencies so moving tests to only run in another env is not going to resolve the issue. – Stretch0 Aug 18 '18 at 09:10

1 Answers1

5

From this answer,

First, you need to "install with all dependencies".

npm install

Then do your tests.

npm test

Then "prune" your dev dependencies as below, as detailed in the docs doing this "will remove the packages specified in your devDependencies".

npm prune --production
Leponzo
  • 624
  • 1
  • 8
  • 20