当前位置:网站首页>NPM Developer Guide

NPM Developer Guide

2020-11-09 08:41:38 XXHolic

Introduction

stay npm Getting started after , Found out npm-developers This document , The introduction begins with 、 Local testing 、 A relatively complete process of packaging to release . Feeling is Getting started A supplement to , So I translated the document into Chinese , Make a deep impression .

original text :npm-developers.

sketch

therefore , You've already planned to use npm To develop ( And it may be released or deployed ) Your project .

fantastic !

On top of the simple steps your users take to install the program , You need to do something .

About these documents

These are the man pages . If you install it npm, You should be able to npm Find documents on specific topics in the manual , Or by npm Help to get the same information .

One package What is it?

One package yes :

1.  One contains a description of the program  package.json  File folder .
2.  There is a  gzip  Compressed packages ( stay  1  On the basis of ).
3.  One is interpreted as (2) In the bag  url.
4.  One that has been published in the registry  <name>@<version>( stay  3  On the basis of ).
5.  A directed (4) Of  <name>@<tag>.
6.  A satisfied one has  “latest”  Labeled  <name>( stay  5  On the basis of ).
7.  A cloned  git  Warehouse link , Point to (1) Folder in .

Even if you've never released your package , If you just want to write a node project , Or after it's packed into a compressed package , You want to be easy to install elsewhere , You can still use npm We have benefited a lot from it .

Git The links to are in the following forms :

git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish

commit-ish It can be anything tag、sha Or be able to provide to git checkout As the branch name of the parameter . The default value is master.

package.json file

You need to have a package.json file , So that we can use npm Do a lot of things . This is basically the whole interface .

Detailed file information , so package.json. The least you need to know is :

  • name: This should be a string that identifies your project . Please do not use name To indicate that the project is based on node Or used JavaScript. You can use it. “engines” Field to indicate your project , What version of node( Or something ), It almost assumes that JavaScript.
    You don't need to match your github Name of Library .
    therefore ,node-foo and bar-js It's a bad name .foo and bar It would be better to name .
  • version:semver Compatible versions .
  • engines: Indicate which version of your project is based on node( Or something else ) function .node API Changes are frequent , The version you rely on , There may be some bug Or new features . Point it out clearly .
  • author: author .
  • scripts: If you have a specific build or install script , So you should put it in scripts In the object . You should have at least one basic smoke test Command as “scripts.test” Field . see npm-scripts.
  • main: If you have a separate module as the entry point to your program ( It's like “foo” Package pass requre("foo") obtain ), Then you need to “main” The field indicates .
  • directories: This is an object that maps names to folders . It's better to include “lib” and “doc”, But if you use “man” Specify a folder full of man pages , They will be installed just like these man pages .

You can go to the root of your project , Use npm init Initialize basic package.json file . For more information see npm-init.

Filter files

Use .npmignore The file excludes something in the package . without .npmignore file , But there are .gitignore file , that npm Matches in the file will be ignored . If you want to include you .gitignore Files excluded from files , You can create an empty .npmignore File over it . And git equally ,npm Look for... In all subdirectories of the package .npmignore and .gitignore file , Not just the root directory .

.npmignore Follow .gitignore Follow the same rule pattern :

  • With # The beginning line will be ignored .
  • The standard global pattern works .
  • You can use forward slash end mode / Specify the directory .
  • You can do this by using the exclamation point ! To negate a pattern .

default , The following paths and files will be ignored , So there's no need to add these to .npmignore in :

  • .*.swp
  • ._*
  • .DS_Store
  • .git
  • .hg
  • .npmrc
  • .lock-wscript
  • .svn
  • .wafpickle-*
  • config.gypi
  • CVS
  • npm-debug.log

Besides ,node_module All the things in will be ignored , Except for binding dependencies .npm Will automatically handle this for you . So there's no need to node_module Add to .npmignore in .

The following paths and files will never be ignored , So add these to .npmignore There's no point in :

  • package.json
  • README (and its variants)
  • CHANGELOG (and its variants)
  • LICENSE / LICENCE

If you consider the structure of the project , Have you found .npmignore It's a maintenance problem , Then try filling in package.json Of documents files attribute , The attribute is an array , The name of the file that should be included in the package or directory . Sometimes white lists are easier to manage than blacklists .

To test your .npmignore Or is the file configuration valid

If you want to reconfirm , When released , Everything you want is in the bag , You can run it locally npm pack command , This command will generate a compressed package in the working directory , Publishing packages also uses this approach .

Link package

npm link Designed to install a development package and view changes in real time , Without having to re install .( Of course , You need to re link or npm rebuidl -g To update the compiled package .)

For practical operation, see Local package debugging :npm link , For document information, see npm-link.

Before release : Make sure your package is up and running

It's important .

If your package cannot be installed locally , You're trying to publish it , There will be problems . Or worse , You can post , But released a broken or meaningless package . So don't do this .

In the root directory of your package , Execute the following command :

npm install . -g

This will tell you if it works . If you prefer to create a link My bag , Point to your working directory , So do it :

npm link

Use npm ls -g See if it exists .

Test local installation , Go to another folder , perform :

cd ../some-other-folder
npm install ../my-package

Elsewhere , Install locally to node_module Under the folder .

Then enter node-repl, Try to use require(“my-thing”) Introduce the main module of your module .

Create a user account

adopt adducer Command to create a user . Run like this :

npm adduser

After that, according to the prompt . Detailed see npm-adduser.

Release your package

This part is very simple . Go to the root of your folder , Execute this :

npm publish

You can give a compressed package , The file name of a compressed package , Or a path to a folder to publish a url.

Please note that , By default , Almost everything in the folder will be made public . therefore , If you have something secret in it , Use one .npmignore The file lists things to ignore globally , Or from a new checkout Release .

Reference material

版权声明
本文为[XXHolic]所创,转载请带上原文链接,感谢