npm
As a gift package that comes with the download node
, you will not be unfamiliar with it.
However, about npm
that, a lot of estimates are just used npm install XXX
as well npm run XXX
.
In fact, there are many interesting commands & parameters here.
Regarding npm
, there are probably two functions:
- It allows us to easily download third-party packages from the Internet to implement functions
- Ability to let us write our own packages and upload them online for others to download
Download related operations
The download mainly revolves around install
this command.
install can be abbreviated as i
Install existing dependencies
When we are under a project, we npm i
can install all the dependencies of the current project by executing.
Contains all dependencies
of , devDependencies
, optionalDependencies
and bundleDependencies
.
If we npm i
add --production
parameters during execution, it means that it is an online environment, devDependencies
and all dependencies under it will be ignored.
Now we have the following package.json
files:
{ "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 --production
parameters, 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 devDependencies
dependencies.
--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 install
addition --no-save
, --save-dev
etc., flag
it will not be written directly into dependencies
it, 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 optionalDependencies by 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.json
can 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.0
will 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
bug
package.json
And we can specify when the package is installed tag
orversion
npm i also@next npm i koa@2.0.0 npm i koa@">=2.0.0 <2.5.0"
Here next
is tag
a default if not specifiedlatest
.
And the second one specifies the installed 2.0.0
version 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:
npm init
createpackage.json
- to develop
npm show <你的包名>
, if 404, then congratulations, the name has not been taken.npm publish
, upload the package.
npm init
In fact, in many cases, we create package.json
only to install dependencies, but npm init
after execution, we have to confirm the optional content many times.
In fact, if you add a parameter, npm
you will not be allowed to confirm these optional contents. npm init -f
, --force
, -y
and --yes
can 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 as
npm ln
Then execute it in the project to be debugged npm link <你的包名>
to create a link that references the local.
After debugging, execute npm unlink
to remove the link.
How to create a command line executable module
We can package.json
add bin
fields to specify a file.
{ "bin": { "sayhi": "bin/hi" } }
file ./bin/hi
:
#!/usr/bin/env node
console.log('hi there')
If it has been used -g
for global installation, the corresponding command will be registered, and we terminal
can execute it directly in it. #!/usr/bin/env node
as 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 XXX
will return the information corresponding to this package, or you can directly follow JSON
the 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 publish
to upload.
npm publish
You can also specify a folder path or a compressed package later, but both need to include package.json
files (the information about the npm package is here)
And , we can --tag=XXX
upload 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 tag
is that we can maintain multiple copies of code at the same time, and the two do not affect each other (but publish
don't forget when you are careful tag
).
For example, last year's node.js
stable version was still 6, but it koa
was already used 7.6+
/ async
features await
, so they released koa@next
, which is now 2.x
used to support the new syntax.
Later version update
If our package is found online and found bug
, we need to fix it, because npm
of the limitations, so the consistency publish
must be guaranteed every time. gives us these three commands:version
npm
npm version patch
npm version minor
npm version major
The three commands will modify version
the 3.2.1
bit in turn.major.minor.patch
patch
patch
bug
For 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 Generator
hug async
/ that is, await
it 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 npm
will directly modify version
and add a commit
record 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 publish
to 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"
%s
it will be automaticallynpm
replaced 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 @latest
top by default
npm scripts
About package.json
China scripts
, I don't know how much you know.
If your package is uploaded to npm
, then there are actually many scripts
similar hooks.
publish
This script will execute when your package is publish
uploaded to the server.
In fact, we can perform git push
operations in this place to push this modification directly to the GitHub
warehouse, saving an unnecessary command typing.
Or if your package is also uploaded to apm
the 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 Flowtype
developed a package, we can directly upload the source code to npm
it, and then install
execute the compilation and removal in the command flow comments
.
Some dependent
node-gyp
packages willinstall: node-gyp rebuild
have operations.
uninstall
If your package will affect some global data (for example, some packages may rewrite .bashrc
and other files).
At this time, you can uninstall
restore those modified items in the script ( conscience operation ).
More
scripts
hooks: https://docs.npmjs.com/misc/scripts
notes
I recently looked through npm
the documentation and found a lot of commands & parameters that were rarely used before.
I think npm
it's really good. Some of the original repetitive work can actually be easily npm
solved 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