Speeding up the npm install

前端 未结 8 1901
忘掉有多难
忘掉有多难 2021-02-01 14:54

I am trying to speed up the npm install during the build process phase. My package.json has the list of packages pretty much with locked revisions in it. I\'ve also configured t

8条回答
  •  鱼传尺愫
    2021-02-01 15:19

    As very modern solution you can start to use Docker. Docker allows you virtualize and pre-define as image the current state of your code, including installed npm-modules and other goodies.

    Once the docker image for your infrastructure/env is built locally, or retrieved from remote repository, it will be stored on the host machine, and you can spin server in seconds. Another benefit of it is that you use same virtualized code infrastructure on any machine where you deploy your code. Docker speeds up install/deployment processes and is widely used technology.

    To start using docker is enough to (all the snippets are just mock/example for pre-setup and are not by any means most robust/elegant solution) :

    1) Install docker and docker-compose using manuals and get some basic understanding of it at docker.com

    2) Write Dockerfile file in root of your application

    FROM node:6.9.5
    RUN mkdir /usr/local/app
    WORKDIR  /usr/local/app
    COPY package.json package.json
    RUN npm install
    

    3) create docker-compose.yml in the root of your project with such content:

    version: "2"
    server:
      hostname: server
      container_name: server
      image: server
      build: .
      command: sh -c 'NODE_ENV=development PORT=8080 node app.js' 
      ports:
        - "8080:8080"
      volumes: #list of folders and files to use 
        - ${PWD}/server:/usr/local/server
        - ${PWD}/app.js:/usr/local/app.js
    

    4) To start server you will need to docker-compose up -d. To see the logs docker-compose logs -f server. If you will restart your server it will do it in seconds once it built the image already at once. Then it will cache build layers locally so next run will take only few seconds.

    I know this might be bit of a robust solution, but I am sure it is have most potential/flexibility and is widely used in industry. And while it requires some learning for anyone who did not use Docker before, in my humble oppinion, it is the best one for your problem.

提交回复
热议问题