问题总结----解决npm install下载[email protected]依赖包超级慢问题

文章目录

  • 一、问题出现原因
  • 二、解决思路
  • 三、解决方案
  • 四、终极方案

一、问题出现原因

使用gitlab-runner部署前端js项目时,下载[email protected]有时候相当的慢。

主要原因是npm install老去国外github下载

> [email protected] install /app/node_modules/chromedriver
> node install.js

Current existing ChromeDriver binary is unavailable, proceding with download and extraction.
Downloading from file:  https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
Saving to file: /app/node_modules/chromedriver/chromedriver/chromedriver_linux64.zip
...

后来去放置gitlab-runner那台主机wget一下,结果一直响应中

cuiyf@gitlabrunner:~$ wget https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip
--2019-08-05 17:46:32--  https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip
Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 216.58.200.48, 2404:6800:4008:801::2010
Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|216.58.200.48|:443... connected.
HTTP request sent, awaiting response... 

二、解决思路

设法使其去淘宝镜像源地址下载

https://registry.npm.taobao.org/

三、解决方案

由于我是用docker部署的,修改Dockerfile添加如下配置
npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver

FROM cuiyf/node:8.12.0-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh
RUN npm config set registry http://172.18.0.1:9000/repository/node-public && \
    npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver && \
    npm install
COPY . .
RUN npm run build
FROM cuiyf/nginx:1.13.12-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

最终效果,快速的去淘宝源下载去了

> [email protected] install /app/node_modules/chromedriver
> node install.js

Current existing ChromeDriver binary is unavailable, proceding with download and extraction.
Downloading from file:  http://cdn.npm.taobao.org/dist/chromedriver/2.46/chromedriver_linux64.zip
Saving to file: /app/node_modules/chromedriver/2.46/chromedriver/chromedriver_linux64.zip
Received 808K...
Received 1652K...
Received 2459K...
Received 3258K...
Received 4092K...
Received 4922K...
Received 5277K total.
Extracting zip contents
Copying to target path /app/node_modules/chromedriver/lib/chromedriver
Fixing file permissions
Done. ChromeDriver binary available at /app/node_modules/chromedriver/lib/chromedriver/chromedriver

四、终极方案

新建一个镜像吧,包括下载node-sassphantomjselectron

FROM node:8.12.0-alpine
LABEL cuiyf 
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache tzdata \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && rm -rf /var/cache/apk/* /tmp/* /var/tmp/* $HOME/.cache \
    && echo "registry=https://registry.npm.taobao.org" > /root/.npmrc \
    && echo "sass_binary_site=https://npm.taobao.org/mirrors/node-sass/" >> /root/.npmrc \
    && echo "phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/" >> /root/.npmrc \
    && echo "electron_mirror=http://npm.taobao.org/mirrors/electron/" >> /root/.npmrc \
    && echo "chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver" >> /root/.npmrc \
    && source /root/.npmrc

你可能感兴趣的:(Javascript,问题总结)