Build NPM private bank (super simple)

Time:2019-10-10

Reason

The reason for my private library is very simple. I’m currently developing a component library for internal use. I don’t want to register NPM or wait for npm’s audit. I just want to have a repository to quickly test and publish my own NPM package.

How to build

At present, the most convenient solution is verdaccio, which is very convenient to build. It is usually completed in a few minutes. The tools needed are:

  • Installing nodejs and NPM
  • Global installation of verdaccio
  • Shh and PM2 (not required, if you want to deploy to a remote server)

Next, the steps of building are described in detail.

Global installation of verdaccio

Before installing verdaccio, I defaulted that everyone had already installed nodejs and NPM environments, which I won’t dwell on any more. If it’s built locally, just do the following. If it is built on a remote server, connect to the remote server via ssh.

# Global Installation
npm install verdaccio - g

Modify verdaccio configuration

The purpose of the configuration modification is to make our private library accessible through public network ip. First, we look at the location of the NPM global installation package.

npm root -g
/usr/local/Cellar/node/8.4.0/lib/node_modules

among/usr/local/Cellar/node/8.4.0/lib/node_modulesThat’s the address where our NPM package is installed globally. Find the location of the configuration file by naming it as follows

Then?

vim default.yaml

The configuration is as follows

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
storage: ./storage
# path to a directory with plugins to include
plugins: ./plugins

web:
  # WebUI is enabled as default, if you want disable it, just uncomment this line
  #enable: false
  title: Verdaccio

auth:
  htpasswd:
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    #max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npmjs.org/

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    proxy: npmjs

  '**':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# You can specify HTTP/1.1 server keep alive timeout in seconds for incomming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enought.
server:
  keepAliveTimeout: 60

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: verdaccio.log, level: info}

# listen
listen: 0.0.0.0:4873

The last additional configuration to support IP access to the extranet

listen: 0.0.0.0:4873

Then enter:wqSave and exit the VIM mode and start the verdaccio service. I usually start through pm2, the reason is very simple, after closing the doc window, the service will not stop, and can manage the service we started well.

Using PM2

Common commands

  • Installation: NPM install pm2-g
  • Start: PM2 start verdaccio
  • Stop: PM2 stop verdaccio
  • Restart: PM2 restart verdaccio
  • Delete application: PM2 delete verdaccio
  • View logs: PM2 logs verdaccio

Let’s start the service and see the effect:

pm2 start verdaccio


So far, NPM’s private library has been set up. In the figure is a newly released component library based on Vue. Later, I will introduce the compilation and release of the component library. Interested friends will pay attention to the following.

Recommended Today

Brief introduction to the use of golang’s HTTP cookie

During the development of server-side programs, cookies are often used to verify that a user is logged in. Golang’s net/ HTTP package comes with the definition of HTTP cookie, the following is a general use of cookies and need to pay attention to the problem. Definition of HTTP cookie Golang’s definition of cookie structure: type […]