# # background

Node.js is simple and easy to get started. But it also lacks a lot of norms, and the level of users is uneven.

One of the most common problems we see these days is that many novices, when deploying, install NPM directly on the server, which is not recommended.


## existing problems

1. Uniqueness cannot be determined

Because the installation is network time-consuming, you can’t even guarantee that the packages from NPM install will be the same on both servers in a cluster.

If a library happens to be updated, and it’s buggy, then you’re just scratching your head: chances are there’s something wrong. It’s a hell of a search.

Of course, many people choose the “locked version” solution to solve this problem.

In the comments section, don’t worry, read the second point below, even the locked version is installed on CI and not on the server.

Whereas the “locked version” belongs to
The poo Bike ProblemI don’t want to discuss it here.


See synonyms at:
Zhihu column – Dead Horse: Why I Don’t Use ShrinkWrap (Lock).

2. It takes a long time to go online and cannot be rolled back quickly

Online, found online fault, to quickly roll back to stop bleeding, meng force:

  1. To wait for the dependency installation, in case there is a jitter or something, no problem P4 failure becomes P0 failure, no year-end bonus.
  2. In case the problem is caused by an underlying dependency, rollback is useless.
  3. npm cacheCan’t solve the problem, like when the machine expands.

## Recommended solution

Zhihu picture viewing experience is poor, please click the larger picture, clear point

The key is to package dependencies at build time.

Advantages:

  • Decompress and start immediately without waiting for network time.
  • It can be guaranteed to run, because at this time the dependencies are determined and guaranteed by CI single test.
  • It can be quickly rolled back to stop bleeding.
  • Packaging can be tar or Docker. (Recommend the latter)

Disadvantages:

  • Large package size (but storage is not valuable…)
Zhihu picture viewing experience is poor, please click the larger picture, clear point

Above is to use
PlantUMLIf you want to have a Markdown like experience with your flowcharts, use PlantUML


How to implement ##?

Then some students have to ask: I am a small company, unlike you have these infrastructure can serve, how to do?

The cost is really low:

  • The repository GitLab comes with CI, so all you need to do is write a configuration file and trigger an automatic build. (or Jenkins)
  • Then put the built files, find a place to store, such as OSS.
  • If the server is deployed, it is best to have an o&M publishing system. If not, write a shell to download and decompress OSS files.
  • Of course, many cloud services support Docker images, which makes it even easier.

# # advertising area

  • For a better reading experience, please visit the Language Version.
  • If you like my post, please Follow my Zhihu and Follow GitHub
  • Guangzhou Ali game, recruit front end, familiar with dynamic effect, Node speed ~