Here are three ways to set up a company’s private NPM repository, each of which has its own advantages.

CentOS Linux release 7.2.1511 (Core)

Tutorial archived on my Github welcome corrections and Star

CNPM structures,

The installation

npm install -g --build-from-source cnpmjs.org cnpm sqlite3
If an error or warning is reported, use the following method to install
npm install -g --unsafe-perm --verbose --build-from-source cnpmjs.org cnpm sqlite3
Copy the code

If the installation is not smooth, install it in the following way:

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

If a warning or installation error is reported, add the parameter –unsafe-perm –verbose

Start and configure the service

Administrator: myname, otherName Range: my-company-name,other-name Default port: 701-registry, 7002-web

Start the service

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

Setting registration Address

Change the default CNPM registration address to a private registration address

cnpm set registry http://localhost:7001
Copy the code

Login CNPM

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

Packages are uploaded to a private warehouse

New project

$ cd /tmp
$ mkdir helloworld && cdHelloworld $CNPM init name: (helloWorld) @my-company-name/ helloWorld version: (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"
}
Copy the code

Upload to a private repository

$CNPM publish + @my-company-name/[email protected]Copy the code

Viewing preview packages

Preview in the browser

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

Preview using CNPM

cnpm info
Copy the code

The installation

All public packages can be installed directly using CNPM

cnpm install hotkeys-js
Copy the code

Built by Verdaccio

Verdaccio is a lightweight private NPM broker registry. (sinopia fork)

The installation

# Install using NPM
npm install -g npm

# Install using YARN
yarn global add verdaccio
Copy the code

Start the service

verdaccio >> verdaccio.log 2>&1 & Start the background and write to the log

# 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.6

verdaccio --listen 4000 --config ./config.yaml # specify the configuration to start
Copy the code

Add a user/login

npm adduser --registry  http://localhost:4873
Copy the code

Uploading private packages

npm publish --registry http://localhost:4873
Copy the code

The registered address is configured locally

npm config list -l Check the default configuration
# change the default address https://registry.npmjs.org/ to private address
npm set registry http://localhost:4873
If you use HTTPS, add the appropriate CA information
# (" null "means to fetch the CA list from the operating system)
$ npm set ca null
Copy the code

Git repositories act as private NPM

This approach benefits from the rich installation methods provided by NPM. Install using the following method:

npm i -S git+ssh://[email protected]:npm/hello.git

npm install -S git+ssh://[email protected]:npm/npm.git# v1.0.27
npm install -S git+ssh://[email protected]:npm/npm# semver: ^ 5.0
npm install -S git+https://[email protected]/npm/npm.git
npm install -S git://github.com/npm/npm.git# v1.0.27
Copy the code

Note: your project must be built under a group for easy management, and the name in package.json must be scoped when generating your package

Create a private module

# Let's say you set up a Git repository and clone it
git clone http://git.your-inc.com/companyfe/hello-private.git

# Generate 'package.json' configuration, note the scope of '@scope'
npm init --scope=companyfe
# Commit to the warehouse
git push origin master
Copy the code

⚠️ will get the following dependencies, note:

The name field must be scoped. Generally, it is the name of the GitLab group, for example, @companyfe. Then, the name is @companyfe/hello-private. Set private to true to prevent private modules from being uploaded to the public network. You need to manually set this parameter.

{
  "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 <[email protected]> (http://wangchujiang.com)"."license": "ISC"
}
Copy the code

Install using private modules

Use NPM Install to install dependencies just like installing open source modules. Private modules are installed in the @scope subfolder, for example: node_modules/@companyfe/hello-private.

# Base installation
npm i -S git+ssh://[email protected]/companyfe/hello-private.git
Git must be tagged with git version information
npm i -S git+ssh://[email protected]/companyfe/hello-private.git# v1.2.0
Copy the code

You get the following dependencies

{
  "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://[email protected]/companyfe/hello-private.git#v1.2.0"
  },
  "author": "kenny wang <[email protected]> (http://wangchujiang.com)"."license": "ISC"
}
Copy the code

Using private modules

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

Advantages and disadvantages

The downside is that private modules cannot be updated using NPM update and can only be reinstalled once. The advantage is that there is no setup service.

The resources

  • Can I run my own private registry?