如何在生产环境中运行构建和测试,当devDependencies未被安装时?

4
我相信这是一个常见的问题,但我似乎找不到确定的答案。
我有一个Node应用程序,在构建时需要一些devDependencies,例如babel。为了运行我的测试,还需要像jest这样的devDependencies。但是当CI在生产环境中运行时,它显然不会安装任何devDependencies,因此我会收到找不到软件包的错误。
如何在生产环境中运行构建和测试而不使用devDependencies是最佳实践?
如果有帮助的话,我正在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

为什么你想要在生产环境中运行测试? - loganfsmyth
因为我没有暂存分支/环境。无论测试如何,我仍然需要运行我的构建,这需要Babel开发依赖项,因此将测试移动到仅在另一个环境中运行不会解决问题。 - Stretch0
1个回答

5
从这个答案中,

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接