Publish packages to the npm registry (medium)

Table of contents

1. Create and publish scoped public packages

1.1 Create scoped public packages 

1.2 Check package contents for sensitive or unnecessary information

1.3 Publish scoped public packages

2. Create and publish private packages 

2.1 Create a private package

2.2 Check package contents for sensitive or unnecessary information

2.3 Publish scoped public packages

3. Package name guide


1. Create and publish scoped public packages

To share code publicly in a user or organization namespace, you can publish public user-scoped or organization-wide packages to the npm registry.

For information about scope, you can refer to the previously written content about modules and packages .

Notice:

1. Before publishing a user-scoped npm package, you must register an npm user account.

2. Additionally, to publish organization-wide packages, you must create an npm user account and then also create an npm organization.

1.1 Create scoped public packages 

1. If you are using npmrc to manage accounts on multiple registries
,
please switch to the corresponding configuration file on the command line:

npmrc <profile-name>

2. On the command line, create a directory for your package:

mkdir my-test-package

3. Switch to the root directory of the package:

cd my-test-package

4. If you use git to manage your package code, in the package root directory, run the following command, which will be git-remote-urlreplaced with the git remote URL of your package:

git init
git remote add origin git://git-remote-url

5. In the package root directory, run npm initthe command and pass the scope to scopethe flag:

  • For org-wide packages, my-orgreplace with your org name:
npm init --scope=@my-org
  • For user-scoped packages, my-usernamereplace with your username:
npm init --scope=@my-username

6. Generate a package.json file in response to the prompt.

7. Create a README file explaining what the package code is and how to use it.

8. In the corresponding editor, write the code for your package.

1.2 Check package contents for sensitive or unnecessary information

To reduce the chance of publishing bugs, we recommend testing packages before publishing them to the npm registry. To test your package, run with the full path to the package directory npm install:

npm install my-package

1.3 Publish scoped public packages

By default, scoped packages are published with private visibility. To publish scoped packages with public visibility, use npm publish --access public.

1. From the command line, navigate to the root directory of the package.

cd /path/to/package

2. To publish your scoped public package to the npm registry, run:

npm publish --access public

3. To view your public package page, visit https;//npmjs.com/package/\*package-name\
 *, replace *package-name* with the name of your package. Public packages are shown below the package name on the npm website public.

2. Create and publish private packages 

To share code with a limited number of users or teams, you can publish private user-wide or organization-wide packages to npm registries.

2.1 Create a private package

1. If you are using npmrc to manage accounts on multiple registries
,
please switch to the corresponding configuration file on the command line:

npmrc <profile-name>

2. On the command line, create a directory for your package:

mkdir my-test-package

3. Switch to the root directory of the package:

cd my-test-package

4. If you use git to manage your package code, in the package root directory, run the following command, which will be git-remote-urlreplaced with the git remote URL of your package:

git init
git remote add origin git://git-remote-url

5. In the package root directory, run npm initthe command and pass the scope to scopethe flag:

  • For org-wide packages, my-orgreplace with your org name:
npm init --scope=@my-org
  • For user-scoped packages, my-usernamereplace with your username:
npm init --scope=@my-username

6. Generate a package.json file in response to the prompt.

7. Create a README file explaining what the package code is and how to use it.

8. In the corresponding editor, write the code for your package.

2.2 Check package contents for sensitive or unnecessary information

To reduce the chance of publishing bugs, we recommend testing packages before publishing them to the npm registry. To test your package, run with the full path to the package directory npm install:

npm install my-package

2.3 Publish scoped public packages

By default, scoped packages are published with private visibility. To publish scoped packages with public visibility, use npm publish --access public.

1. From the command line, navigate to the root directory of the package.

cd /path/to/package

2. To publish your scoped public package to the npm registry, run:

npm publish

3. Package name guide

When naming the package, choose

  • only
  • descriptive
  • Comply with npm policy guidelines, for example, don't give a package an offensive name, and don't use someone else's trademarked name or violate the npm trademark policy.

Also, when choosing a name for an unscoped package
, choose:

  • not used by others
  • is spelled differently than other package names
  • Do not confuse the identities of other authors

Guess you like

Origin blog.csdn.net/u014388408/article/details/131886419