Nodejs advanced tutorial, small white detour!!

NPM usage tips and best practices

Prerequisite: Ensure that Node.js is installed

Best practices for NPM

NPM install is the most common NPM CLI command, but it has more power! Next you’ll see how NPM helps you throughout the entire application lifecycle – from creating a project to developing and deploying it

Know the NPM

Before we begin, let’s take a look at some commands to view the version of NPM that is running

npm version

To get the current version of the NPM CLI in use, you can run the NPM version command. This command returns many other information besides the version – the current package version, the version of Node. js you are using, the version of openSSL or V8.

npm help

NPM, like most command-line tools, has a built-in help function that can get a description and feed of the command

1 the use ofnpm initCreating a new project

Using the NPM init command helps you interactively create package.json files that pop up with project name and description problems. There is a quick fix to NPM init –yes(or NPM init-y for short) so that no problems pop up. Just create a package.json with the default configurations, which you can configure using the following commands

npm config set init.author.name YOUR_NAME
npm config set init.author.email YOUE_EMAIL
Copy the code

2 Search for the correct NPM package

Find the right NPM are quite challenging – tens of thousands of packages to choose from, so choosing the right NPM package very distressing, however, we can choose a module to help us to send the HTTP request has a website that can help us simply complete the task, it is NPMS. IO, it will show the quality of the package, popularity, And maintainability, which is calculated by combining whether a module has updated dependencies, whether it has prompted configurations, whether it has done test coverage, and whether it has recently committed records

3 Check the NPM package

Once we find our module (in our case, a Request module), we should look at the documentation and issues to better understand the packages we are introducing into the application, and remember that the more NPM packages you use, the higher the risk for your project

If you want to open the module’s home page on the CLI, do so

npm home request

Open the issues of the module

npm bugs request

Or if you just want to view your module’s Git repository, execute the following command

npm repo request

4 Saving dependencies

NPM is controlled by SEMver

SEMVER refers to semantic version, and its rules are summarized as follows: Version format: Major version number. The second version number. The increment rule is as follows:

  • Major version number: new architecture adjustments that are incompatible with older versions
  • Minor version: New functions and compatible with earlier versions
  • Revision number: Bug fixes, compatible with older versions.

Once you have decided to introduce a third-party package into your project, you must install and save it. The most common command is NPM install some-package. If you want to automatically add package information to package.json, You need to execute NPM install some-package –save

NPM will save your package with the prefix ‘^ ‘by default. The installation rules are as follows:

^version

Compatible with a certain version The left-most non-0 digit in the version number can be arbitrary to the right. If a version number is missing, the position of the version number can be arbitrary, for example, ^1.1.2, which indicates >=1.1.2<2.0.0, 1.1.2, 1.1.3, or..... , 1.1.n, 1.2.n,..... , n.n, such as: ^ 0.2.3, said > = 0.2.3 < 0.3.0, can be 0.2.3, 0.2.4,... , 0.2 n, such as: ^ 0.0, said > = 0.0.0 < 0.1.0 from, can be 0.0.0, 0.0.1,... , 0.0 nCopy the code

NPM config set save-prefix=’~’

The installation rules for this package prefix are as follows:

~version

If the minor version is specified, the minor version stays the same and the patch version is arbitrary. If the minor and patch versions are not specified, the minor and patch versions are arbitrary. ~1.1.2: >=1.1.2 <1.2.0, can be 1.1.2, 1.1.3,..... , 1.1 n, such as: ~ 1.1, said > = 1.1.0 < 1.2.0, such as: ~ 1, said > = 1.0.0 < 2.0.0, can be a 1.0.0, 1.0.1,... , 1.1.n, 1.2.n,..... , 1. N.nCopy the code

If you want to save the specified version number, run NPM config set save-exact true

5 Locking Dependencies

As described above, NPM uses semver, a semantized version number for control, which makes it easy to get and upgrade dependencies during development, but lax version number control also creates uncertainty

  • NPM recommends using semver versions, but some packages do not follow Semver;
  • Package. json can control your direct dependencies with precise version numbers, but third-party dependencies cannot be managed.
  • The version of the package executed during the development phase may not be the same as the version of the package later in the deployment, and the third party packages you rely on May also have the potential to go live.

NPM shrinkwrap can be used to solve this problem. This command generates an npm-shrinkwrap. This file contains not only the specified version of the package installed on your machine, but also the dependencies for the specified version of the package. If you run the NPM install command, you will regenerate the same dependency tree

6 Check whether the dependency packages of the project are updated

NPM has a built-in tool method called NPM outdated to check for outdated packages. Running ‘NPM outdated’ in your project is ok, but manually checking for updates is boring. There is a tool called Greenkeeper that automatically checks for outdated packages. If you are interested, you can click on the link.

7 development kit

When developing the NPM package locally, we can use the NPM link command to link the NPM module to the corresponding running project, which is convenient for debugging and testing the module

Can you give me an example of what it means and how to use it

We now have two projects, namely NPm-link-module and NPm-link-test. Npm-link-module is the NPM package we want to develop, and NPm-link-test is used to test the package we developed

NPM init-y on npm-link-module generates a default package configuration file package.json, creates a new directory named index.js as follows, and writes a simple date conversion function

function formateTime(date) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDay();
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    return year + The '-' + month + The '-' + day;
}
module.exports = formateTime
Copy the code

After executing the command, npm-link-module will be linked globally according to the package.json configuration. The path is {prefix}/lib/node_modules/ , as described in the official documentation. We can use the NPM config get prefix command to get the value of prefix (I’m using Windows, the actual link path is {prefix}/node_modules/ ).

NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module

npm-link-module

Create an index.js file under npm-link-test

let formateTime = require('npm-link-module');
let date = new Date(a);console.log(formateTime(date));
Copy the code

After executing node index.js, you can see that the command line has printed the result of the function’s execution

Then, we modify the date function in the index.js file in npm-link-module to return the date in slash format

return year + '/' + month + '/' + day;
Copy the code

Return to npm-link-test to execute index.js, and you can see that the result has been changed to a slash date

As you can see, all changes to npm-link-module are mapped directly to npm-link-test/node_modules/npm-link-module

To clarify, I plan to write about 10 advanced NodeJS tutorials. Now I have a general framework in mind, so I am ready to comb through it while writing, and I will list the contents of the tutorials after writing