Based on Windows platform, verdaccio is used to build an Intranet NPM private server to manage the NPM package of the company’s front-end team.

Verdaccio installation and related configuration

1.1 installation

Installing Verdaccio using NPM requires a global installation, so be aware of permissions.

npm install -g verdaccio
Copy the code

1.2 run

Use the verdaccio command to start running

verdaccio
Copy the code

The results

Warn - config file - C: / Users/Administrator/AppData/Roaming verdaccio/config. # yaml configuration file path warn - the Plugin successfully loaded: htpasswd warn --- Plugin successfully loaded: Audit WARN -- HTTP address - http://localhost:4873/ - verdaccio/4.4.0Copy the code

To open the NPM repository address, visit http://localhost:4873/

1.3 configuration

By running the results, find the configuration file path C: / Users/Administrator/AppData/Roaming verdaccio/config. Yaml

# # 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: D:/npm-repo # path to a directory with plugins to include plugins: ./plugins web: title: Verdaccio # comment out to disable gravatar support # gravatar: false # by default packages are ordercer ascendant (asc|desc) # sort_packages: asc # convert your UI to the dark side # darkMode: true # translate your registry, api i18n not available yet # i18n: # list of the available translations https://github.com/verdaccio/ui/tree/master/i18n/translations # web: en-US 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 unpublish: $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/publish packages # (anyone can register by default, remember?) publish: $authenticated unpublish: $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 incoming 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 enough. server: keepAliveTimeout: 60 middlewares: audit: enabled: true # log settings logs: - { type: stdout, format: pretty, level: http } #- {type: file, path: verdaccio.log, level: info} #experiments: # # support for npm token command # token: false # # support for the new v1 search endpoint, functional by incomplete read more on ticket 1732 # search: false # # disable writing body size to logs, read more on ticket 1912 # bytesin_off: False # This affect the Web and API (not developed yet) #i18n: #web: en-us listen: 0.0.0.0:4873Copy the code

The following parameters need to be configured

1.3.1 storage

NPM package storage address. You can create a directory on the local disk and store the NPM package separately.

# path to a directory with all packages
storage: D:/npm-repo
Copy the code

1.3.2 uplinks

If it is not found in the local NPM package, install is installed in another repository by default and the default configuration is pulled from the NPM official.

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

1.3.3 packages

Set the permission to publish and use the package, whether to proxy to the public NPM repository, etc

packages:
  '@xdh/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated

  '**':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs
Copy the code

The package with the @xdh prefix is private and will not be external. Proxy: NPMJS specifies that if the package is uploaded, it will be proxyed to the NPM public repository. If a package is downloaded without the @xdh prefix, it will be automatically proxyed to the NPM public repository to find the resource and download it. By default, the pulled resource will be cached in the storage folder we specified earlier.

1.3.4 listen

Set the Intranet access and port number. The Intranet can be accessed using the local IP address

Listen: 0.0.0.0:3000Copy the code

After setting, restart Verdaccio and you can access it.

Ii. Pm2 management services

Pm2 is used for service management in order to enable the service to run in the background without being affected by window closing

2.1 installation pm2

npm install pm2 -g
Copy the code

2.2 run verdaccio

Run pm2 start verdaccio directly. If it cannot be started, use the following method

// Run pm2 start in global node_modules verdaccio\bin\verdaccio C:\Users\Administrator\AppData\Roaming\npm\node_modules\verdaccio\bin\verdaccioCopy the code

2.2 Viewing the Running Status

Check the running status status: online: indicates that the system starts normally and can be accessed

pm2 list
Copy the code

2.3 Stopping operation

pm2 stop verdaccio
Copy the code

Running Status Status: stoped

2.4 Deleting a Service

pm2 delete verdaccio
Copy the code

NRM manages NPM sources

Generally, multiple NPM sources are installed locally. You can use NRM to manage and switch them.

3.1 installation

npm install nrm -g
Copy the code

3.2 Adding a Local NPM Source

nrm add localnpm *.*.*.*:4873
Copy the code

3.4 Checking whether the vm is added successfully

NRM ls / / source list * NPM yarn -- -- -- -- -- -- -- -- https://registry.npmjs.org/ -- -- -- -- -- -- -- https://registry.yarnpkg.com/ CNPM -- -- -- -- -- -- - http://r.cnpmjs.org/ taobao ----- https://registry.npm.taobao.org/ nj --------- https://registry.nodejitsu.com/ npmMirror -- https://skimdb.npmjs.com/registry/ edunpm ----- http://registry.enpmjs.org/ localnpm --- http://*.*.*.*:4873/Copy the code

3.5 Switching to the Local Source

NRM use localnpm / / source list NPM yarn -- -- -- -- -- -- -- -- https://registry.npmjs.org/ -- -- -- -- -- -- -- https://registry.yarnpkg.com/ CNPM -- -- -- -- -- -- -  http://r.cnpmjs.org/ taobao ----- https://registry.npm.taobao.org/ nj --------- https://registry.nodejitsu.com/ npmMirror -- https://skimdb.npmjs.com/registry/ edunpm ----- http://registry.enpmjs.org/ * localnpm --- http://*.*.*.*:4873/Copy the code

Create a local NPM user

To create a local NPM user for uploading and managing NPM packages, run the NPM adduser command and enter the user name, password, and email address.

npm adduser
Username: yourname
Password: *
Email: *
Copy the code

Record the set user information, which will be used in the next upload section.

5. Create and upload the NPM package

Create the @xdh/test project, add the index.js file in the root directory of the project, initialize the project by NPM, and enter the prompt message

npm init 
Copy the code

Upload to the local repository and run the following command in the project root directory:

// login to NPM. // enter your account, password, and email address as prompted. NPM loginCopy the code

Refresh the local link and see that the NPM package has been published successfully.

Check whether the strage file is saved successfully

Install local dependencies

Ensure that the current NPM source is switched to LocalNPM

npm install @xdh/test -S
Copy the code

At this point, the Intranet NPM private server has been set up and private packages can be uploaded and used.