Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a private npm server using sinopia

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use sinopia to build a private npm server, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

Brief introduction

Effect display

The advantage of using sinopia is that it is very convenient for node engineers to use their own npm packages when working internally; in addition, sinopia, which caches downloaded packages, can speed up the execution of npm install-related commands to a considerable extent.

At work, there are indeed scenarios where sinopia is needed as a private npm server. I used to turn on sinopia on my computer all the time. The biggest problem with this is that if the sinopia is kept on in the background, it will consume more and more resources, and the computer will eventually become very stuck. Occasionally, it will bring all kinds of inconvenience because you forget to turn sinopia on or off.

Today, I tried to build a sinopia service directly on the raspberry pie. The final effect is more complete and basically meets the needs. It includes features such as user rights management, access to the public network using custom domain names, and self-startup of sinopia services.

Note: the following shell command is executed by default in the shell of the raspberry pie, not on the local computer.

Install the latest long-term support version of node environment

Raspberry pie comes with a node environment of v4.8.2, which needs to be upgraded.

Install nvm

It is recommended that you install nvm to facilitate the management of multiple versions of the node environment.

# install nvmwget-qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash# restart shell# verify nvm install command-v nvm

Use nvm to install the latest long-term supported node environment

# install Nodenvm install-- lts# verify installation-- > v8.9.1node-v

Install and configure sinopia

Install sinopia

# install npm install-g sinopia# to verify whether the installation is successful-> this step outputs information such as the automatically generated configuration file path. Sinopia

Resolve port 4873 occupancy problem

When sinopia starts, port 4873 is used by default, and port conflicts may be encountered.

# install the lsof command sudo apt-get updatesudo apt-get install lsof# to view the port occupancy process PIDlsof-I: 487kill the process occupying port 4873. 4649, to be replaced with the actual PID. Kill-9 4649

Sign up for a default account

To improve security, we will disable sinopia's user registration feature later, so sign up for a default sinopia account first. You need to issue the sinopia command in the current shell to start the service, and then re-open a shell execution:

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

User names, passwords, mailboxes, etc., should be kept in mind and the complexity of the appropriate settings.

# upgrade and install vim

It feels like the vim that comes with raspberry pie doesn't work very well, so I upgraded it by the way.

`

# install vim

Sudo apt-get update

Sudo apt-get install vim

# configuration supports right mouse button replication in vim

Vim / .vimrc

`

Add the following line to the .vimrc file:

Shell set mouse=v

Configure sinopia

The configuration file path can be viewed from its output when the sinopia command is executed, usually / home/pi/.config/sinopia/config.yaml

Based on my experience and documentation, the following are mainly configured:

Max_users:-1: disable registration.

* npmjs: url: https://registry.npm.taobao.org*: set the npm image to Taobaoyuan to speed up the installation of npm public packages, and make use of the read-only feature of Taobaoyuan to avoid mistakenly publishing private npm packages to the public network.

Access: $authenticated: access by anonymous users is prohibited. After configuration, non-logged-in users do not see any information about private packages on the sinopia.

Max_body_size: '200mbpackages: this setting will improve the success rate of installing super npm packages, such as react-native.

The complete configuration is as follows. If you don't configure it on a raspberry pie, replace / home/pi with your real user pathname.

# # 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/rlidwka/sinopia/tree/master/conf## path to a directory with all packagesstorage: / home/pi/.local/share/sinopia/storageauth: htpasswd: file:. / htpasswd # Maximum amount of users allowed to register, defaults to "+ inf". # You can set this to-1 to disable registration. Max_users:-Mr. a list of other known repositories we can talk touplinks: npmjs: url: https://registry.npm.taobao.orgpackages:'@ * / *': # scoped packages access: $authenticated publish: $authenticated'*': # 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: $authenticated # 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# log settingslogs:-{type: stdout, format: pretty, level: http} #-{type: file, path: sinopia.log, level: info} max_body_size:' 200mb'

You can modify the configuration in the local editor and copy it directly to the raspberry pie:

# Open the configuration file vim / home/pi/.config/sinopia/config.yaml

Use the paste command. Paste directly, the format will be out of order.

Set pastei# right-click and paste.

Configure frpc remote access

For more information on the configuration of frp, please see [Tips to solve Big problems] use frp to break the limitation that the inelastic public network IP of Ali CVM cannot be used as a Web server. Only key configuration changes for frpc are posted here:

[web-sinopia] type = httplocal_port = 4873subdomain = sinopia

Using Systemd to realize self-booting of sinopia Service

Raspberry pie, with Systemd by default, can be used directly:

Sudo vim / usr/lib/systemd/system/sinopia.service

The details of sinopia.service are as follows, where / home/pi/.config/sinopia/config.yaml should be replaced with your own config.yaml address:

[Unit] DescrIPtion=sinopiaAfter= network.target [service] TimeoutStartSec=30ExecStart=/home/pi/.nvm/versions/node/v8.9.1/bin/sinopia / home/pi/.config/sinopia/config.yamlExecStop=/bin/kill $MAINPIDRestart=on-failureRestartSec=42s [Install] WantedBy=multi-user.target

Start sinopia and set up boot:

Systemctl enable sinopiasystemctl start sinopiasystemctl status sinopia

Other commands that may be useful

# disable Service systemctl disable sinopia# and restart Service systemctl restart sinopia

Use the sinopia private npm server feature on another computer

Assume that the public network address of the final sinopia server is: * http://sinopia.example.com*

If you really want to use it, you need to configure it in the terminal:

Npm set registry http://sinopia.example.comnpm adduser-- registry http://sinopia.example.comnpm login

Once configured, you can try to publish a private npm package:

# initialize a new npm package in a folder npm init# publish to a private sinopia server: npm publish

After the release is successful, log in to * http://sinopia.example.com* in the browser, refresh the page, and you should be able to see the package you just released.

Note that other users should also log in first when using packages on private libraries, otherwise they will report an error:

Unregistered users are not allowed to access package thank you for reading this article carefully. I hope the article "how to build a private npm server using sinopia" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report