NPM Practical Guide North

npmAs a gift package that comes with the download node, you will not be unfamiliar with it.

However, about npmthat, a lot of estimates are just used npm install XXXas well npm run XXX.

In fact, there are many interesting commands & parameters here.
Regarding npm, there are probably two functions:

  1. It allows us to easily download third-party packages from the Internet to implement functions
  2. Ability to let us write our own packages and upload them online for others to download

Download related operations

The download mainly revolves around installthis command.

install can be abbreviated as i

Install existing dependencies

When we are under a project, we npm ican install all the dependencies of the current project by executing.
Contains all dependenciesof , devDependencies, optionalDependenciesand bundleDependencies.
If we npm iadd --productionparameters during execution, it means that it is an online environment, devDependenciesand all dependencies under it will be ignored.

Now we have the following package.jsonfiles:

{
  "dependencies": {
    "koa": "^2.5.0"
  },
  "devDependencies": {
    "eslint": "^4.19.1"
  }
}

 

If executed npm i, all dependencies will be installed.

> npm ls --depth=0
├── eslint@4.19.1
└── koa@2.5.0

 

Then we try to add --productionparameters, and the --only=prod[uction]same effect can be achieved.

> npm ls --depth=0
└── koa@2.5.0

 

Use --only=dev[elopment]is used to install only devDependenciesdependencies.

--depth=XXX is used to set the depth of the display path. By default, all dependencies will be printed recursively.

Install new dependencies

The above is the operation of directly installing the original dependencies of the project. If we want to add some dependencies, here are some options to understand.
If we perform installaddition --no-save, --save-devetc., flagit will not be written directly into dependenciesit, but there will be some other processing.

Various options:

flag description
--save-prod Default option corresponds todependencies
--no-save Do not write dependencies intopackage.json
--save-dev-D corresponddevDependencies
--save-optional-O Correspondingly , the dependencies under this module can be ignored optionalDependenciesby specifying during installation--no-optional
--save-bundle-B Corresponding bundleDependencies, seems to have been abandoned -.-
--save-exact-E ^Install a precise version, and no identifiers such as the version number will be added .
--global-g Global installation package, generally requires administrator privileges

For more parameters, please refer to: https://docs.npmjs.com/cli/install

You package.jsoncan often see an ^or in front of the version number of a dependency ~.
^and ~will cause the latest version to be installed according to the rules when reinstalling dependencies.
For example , if the version number is , all packages ^2.1.0will be matched . If the version number is , all packages will be matched . You need to re-upload your own. Therefore, when it comes to updates that may have incompatible consequences, please be sure to modify the first two version numbers. . Otherwise it's a scam -.->=2.1.0 <3.0.0
~2.1.0>=2.1.0 <2.2.0
bugpackage.json

And we can specify when the package is installed tagorversion

npm i also@next
npm i koa@2.0.0
npm i koa@">=2.0.0 <2.5.0"

 

Here nextis tag a default if not specifiedlatest .
And the second one specifies the installed 2.0.0version koa.
The last one selects the latest version to install within the scope rule.

Upload related operations

To upload, it must be developed first.
So the whole process of our upload is probably like this:

  1. npm initcreatepackage.json
  2. to develop
  3. npm show <你的包名>, if 404, then congratulations, the name has not been taken.
  4. npm publish, upload the package.

npm init

In fact, in many cases, we create package.jsononly to install dependencies, but npm initafter execution, we have to confirm the optional content many times.
In fact, if you add a parameter, npmyou will not be allowed to confirm these optional contents.
npm init -f, --force, -yand --yescan achieve this effect.
Of course, if you want to send this folder as one pacakge, these optional fields are all required.

The process of developing the package

How to debug locally

During the development process, in order to quickly debug locally, you can execute such a command:
npm link <你的包名>
If you are in the folder of the current package, you can execute it directlynpm link

can be abbreviated asnpm ln

Then execute it in the project to be debugged npm link <你的包名>
to create a link that references the local.
After debugging, execute npm unlinkto remove the link.

How to create a command line executable module

We can package.jsonadd binfields to specify a file.

{
  "bin": {
    "sayhi": "bin/hi"
  }
}

 

file ./bin/hi:

#!/usr/bin/env node

console.log('hi there')

 

If it has been used -gfor global installation, the corresponding command will be registered, and we terminalcan execute it directly in it.
#!/usr/bin/env nodeas required, the path may change

npm show

This method does not seem to be written in the documentation. . But it does exist.
The execution npm show XXXwill return the information corresponding to this package, or you can directly follow JSONthe format to take the value later:

npm show koa version
npm show koa dist-tags.latest

 

npm publish

When our package is developed, it can be executed publishto upload.

npm publish

 

You can also specify a folder path or a compressed package later, but both need to include package.jsonfiles (the information about the npm package is here)

And , we can --tag=XXXupload a corresponding one by splicing it later tag. If we don't write it, it will be uploaded to the next by default latest.
The advantage of writing tagis that we can maintain multiple copies of code at the same time, and the two do not affect each other (but publishdon't forget when you are careful tag).
For example, last year's node.jsstable version was still 6, but it koawas already used 7.6+/ asyncfeatures await, so they released koa@next, which is now 2.xused to support the new syntax.

Later version update

If our package is found online and found bug, we need to fix it, because npmof the limitations, so the consistency publishmust be guaranteed every time. gives us these three commands:version
npm

  1. npm version patch
  2. npm version minor
  3. npm version major

The three commands will modify versionthe 3.2.1bit in turn.
major.minor.patch

patch

patchbugFor the smallest changes, the fixes we mentioned above , ^and ~the version numbers that will be compatible with each other.

minor

If it is minor, the version number in the middle will be modified. Generally speaking, the addition of new functions needs to modify this version number, because the previous usage may be changed.

major

The last one is the version number that will be modified only in a big update, such as our dear koa, after abandoning the Generatorhug async/ that is, awaitit is released directly koa2.x.

The prerequisite for executing these three commands is that there are no uncommitted changes in your current repository.
Because it npmwill directly modify versionand add a commitrecord for you, if there are uncommitted modifications, it may cause version conflicts.
After executing these commands (or manually modifying the version number is no problem), and then execute npm publishto upload the update package.

If you want to customize the information submitted this time, you can do this:
npm version patch -m "Upgrade version to %s"
%sit will be automatically npmreplaced with the updated version number and submitted.

Be sure to remember to add the corresponding when updating --tag, otherwise it will be pushed to the @latesttop by default

npm scripts

About package.jsonChina scripts, I don't know how much you know.
If your package is uploaded to npm, then there are actually many scriptssimilar hooks.

publish

This script will execute when your package is publishuploaded to the server.
In fact, we can perform git pushoperations in this place to push this modification directly to the GitHubwarehouse, saving an unnecessary command typing.
Or if your package is also uploaded to apmthe same type of warehouse or the like, you can also process it directly here (saving many tedious operations)

install

This script will be executed after the package is installed.
For example, if we Flowtypedeveloped a package, we can directly upload the source code to npmit, and then installexecute the compilation and removal in the command flow comments.

Some dependent node-gyppackages will install: node-gyp rebuildhave operations.

uninstall

If your package will affect some global data (for example, some packages may rewrite .bashrcand other files).
At this time, you can uninstallrestore those modified items in the script ( conscience operation ).

More scriptshooks: https://docs.npmjs.com/misc/scripts

notes

I recently looked through npmthe documentation and found a lot of commands & parameters that were rarely used before.
I think npmit's really good. Some of the original repetitive work can actually be easily npmsolved by using related commands.
I hope you don't just use it npm install.
Finally: NPM Loves You.

References

https://docs.npmjs.com/cli/init
https://docs.npmjs.com/misc/developers
https://docs.npmjs.com/cli/version

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324590474&siteId=291194637