三种搭建npm仓库的方法

2019-12-19 16:18:02

参考地址 在5分钟内搭建企业内部私有npm仓库

下面通过三种方法来搭建公司私有npm仓库,每种方式都有自己的优势。

Node.js >= 6.11.3,我的Node版本:node v8.2.1
Linux or OSX,我的系统版本:CentOS Linux release 7.2.1511 (Core)

教程归档在我的Github中欢迎修正和Star

cnpm搭建

安装

npm install -g --build-from-source cnpmjs.org cnpm sqlite3# 如果报错或者警告通过下面方式安装npm install -g --unsafe-perm --verbose --build-from-source cnpmjs.org cnpm sqlite3

如果安装不流畅通过下面形式安装:

npm install -g --build-from-source \
  --registry=https://registry.npm.taobao.org \
  --disturl=https://npm.taobao.org/mirrors/node \
  cnpmjs.org cnpm sqlite3

如果报警告或者安装错误,请添加参数--unsafe-perm --verbose

启动并配置服务

管理员:myname,othername
范围:my-company-name,other-name
默认端口:7001-registry, 7002-web

启动服务

$ nohup cnpmjs.org start --admins='myname,othername' \
  --scopes='@my-company-name,@other-name' &

设置注册地址

将cnpm默认注册地址更改为私有注册地址

cnpm set registry http://localhost:7001

登录cnpm

$ cnpm login
Username: myname
Password: ***
Email: (this IS public) test@test.com

包上传到私有仓库

新建项目

$ cd /tmp
$ mkdir helloworld && cd helloworld
$ cnpm init
name: (helloworld) @my-company-name/helloworldversion: (1.0.0)

{  "name": "@my-company-name/helloworld",  "version": "1.0.0",  "description": "my first scoped package",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"
  },  "author": "",  "license": "ISC"}

上传到私有仓库

$ cnpm publish
+ @my-company-name/helloworld@1.0.0

查看预览包

浏览器中预览

open http://localhost:7002/@my-company-name/helloworld

使用cnpm预览

cnpm info

安装

所有公共包都可直接使用cnpm安装

cnpm install hotkeys-js

通过verdaccio搭建

verdaccio 是一个轻量级的私有npm代理注册。(sinopia fork)

安装

# 使用 npm 安装npm install -g npm# 使用 yarn 安装yarn global add verdaccio

启动服务

verdaccio >> verdaccio.log 2>&1 & # 后台启动并写入日志# Verdaccio doesn't need superuser privileges. Don't run it under root.# warn --- config file  - /root/.config/verdaccio/config.yaml# warn --- http address - http://localhost:4873/ - verdaccio/2.3.6verdaccio --listen 4000 --config ./config.yaml # 指定配置启动

添加用户/登录

npm adduser --registry http://localhost:4873

上传私有包

npm publish --registry http://localhost:4873

本地配置注册地址

npm config list -l # 查看默认配置# 将默认地址 https://registry.npmjs.org/ 改成私有地址npm set registry http://localhost:4873# 如果您使用HTTPS,请添加适当的CA信息#(“null”表示从操作系统获取CA列表)$ npm set ca null

Git仓库当私有npm

这个方法得益于,npm提供的的丰富安装方法。通过下面方法安装:

npm i -S git+ssh://git@git.showgold.cn:npm/hello.git

npm install -S git+ssh://git@github.com:npm/npm.git#v1.0.27npm install -S git+ssh://git@github.com:npm/npm#semver:^5.0npm install -S git+https://isaacs@github.com/npm/npm.git
npm install -S git://github.com/npm/npm.git#v1.0.27

⚠️ 上面安装需要注意:你的工程一定是在某一个组下面建立,方便管理,在生成你的包的时候package.json中的name一定要带上范围

建立一个私有模块

# 假设你建立了一个Git仓库,先克隆下来git clone http://git.your-inc.com/companyfe/hello-private.git# 生成 `package.json` 配置, 注意限定 `@scope` 范围npm init --scope=companyfe# 提交到仓库git push origin master

⚠️ 将得到如下依赖,注意:

name字段必须限定范围,一般为 GitLab group 的名字, 例如 @companyfe, 那么 name 为: @companyfe/hello-private
private 设为 true 防止将私有模块上传到公网上去,需要手动设置一下。
{  "name": "@companyfe/hello-private",  "version": "1.0.1",  "description": "",  "main": "index.js",  "private":true,  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"
  },  "author": "kenny wang <wowohoo@qq.com> (http://wangchujiang.com)",  "license": "ISC"}

安装使用私有模块

跟安装开源的模块一样, 使用 npm install 安装依赖即可. 私有模块会安装在 @scope 的子文件夹中, 例如: node_modules/@companyfe/hello-private.

# 基础安装npm i -S git+ssh://git@git.your-inc.com/companyfe/hello-private.git# 带版本信息的,必须通过 git 打 tagnpm i -S git+ssh://git@git.your-inc.com/companyfe/hello-private.git#v1.2.0

将得到如下依赖

{  "name": "helloworld",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"
  },  "dependencies": {    "@companyfe/hello-private": "git+ssh://git@git.your-inc.com/companyfe/hello-private.git#v1.2.0"
  },  "author": "kenny wang <wowohoo@qq.com> (http://wangchujiang.com)",  "license": "ISC"}

使用私有模块

var hello = require('@companyfe/hello-private');

优劣势

不好的地方是,使用 npm update 是无法更新私有模块,想更新只能重新安装一次。好处是不用搭建服务。


  • 2018-08-18 20:30:12

    laravel5.5 路由分割成不同文件

    routes.php/api.php文件用来放置laravel路由,当项目越来越大,相应的路由文件也会越来越多。如果能够将不同功能的路由分割到不同的文件,那么对以后的维护将很有帮助。

  • 2018-08-20 15:26:19

    关于OnTouch 和OnClick同时调用冲突的解决方案

    大家在搞轮播图的时候会碰到这样的情况,点击进入webview界面,长按轮播图停止轮播,手松开图又开始轮播,这里就涉及到了OnTouch 和OnClick同时调用。两者是有冲突的。这里简单介绍,给大家提供思路。

  • 2018-08-20 15:29:11

    揭开RecyclerView的神秘面纱(二):处理RecyclerView的点击事件

    主要讲述了RecyclerView的基本使用方法,不同的布局管理器而造成的多样化展示方式,展示了数据之后,一般都会与用户进行交互,因此我们需要处理用户的点击事件。在ListView和GridView提供了onItemClickListener这个监听器,然而我们查找RecyclerView的API却没有类似的监听器,因此我们需要自己手动处理它的点击事件。 以下提供两种方法来实现处理RecyclerView点击事件的功能,以下代码均基于上一篇文章的代码做出修改。

  • 2018-08-20 22:58:46

    onInterceptTouchEvent和onTouchEvent调用关系详解 ...

    老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料,才知道是怎么回事,这里总结一下,记住这个原则就会很清楚了:

  • 2018-08-23 15:32:18

    map对象拷贝问题

    最后面是使用序列化的方式,发现,更改引用类型的数据的时候,mapNew对象并没有发生变化,所以产生了深拷贝。 上述的工具类,可以实现对象的深拷贝,不仅限于HashMap,前提是实现了Serlizeable接口。

  • 2018-08-24 11:33:17

    总结和分析几种判断 RecyclerView 到达底部的方法

    SwipeRefreshLayout 写一个 RecyclerView 的上下拉 ,里面有一个判断 RecyclerView 是否到达底部的方法 isBottom。我的同事用了这个上下拉之后发现有些小 bug,没考虑周全,譬如各个子项高度不统一的时候,然后我找到原因是因为这个判断上下拉的问题。所以,我就去网上查到几种判断 RecyclerView 到达底部的方法,发现各有千秋。以下的分析都以上一篇文章的 SwipeRecyclerView 为例

  • 2018-08-26 00:18:04

    RecyclerView 图片错位空白的问题

    1.图片错位的原因是因为图片异步记载返回去展示出的问题。图片空白,是item刷新,请求图片时间上的问题。 2。viewHolder.setIsRecyclable(false); 就没有tag,不设置 就有tag,但是有没有没啥区别 设置tag,

  • 2018-08-28 10:00:24

    laravel使用队列的简单步骤

    最近需要导入大量的excel文件,数量达到十万之多。 而我又不想修改服务器的超时时间,因为这样可能影响服务器的堵塞。 而php又没有很好的异步。 后来发现了令laravel最为骄傲的部分,队列。具体文档参考下方链接。