Session Notes: Getting npm Scoped Packages Right

These are the written notes for a talk we give on scoped package names, the @scope/name form on npm. There is no recording or sign-up; the notes are the whole thing. They follow the talk's agenda, then collect the audience-style questions that come up most when teams first move their libraries under a scope.

Agenda

  1. Why scopes exist
  2. Naming and ownership rules
  3. The first-publish access trap
  4. Scopes, private registries and dependency confusion
  5. Moving an existing package into a scope
  6. Questions

Key points from the talk

1. Why scopes exist

The unscoped npm namespace is shared by everyone, so good short names are long gone and lookalike names are a constant risk. A scope is a namespace owned by a user or an organisation. Only members can publish under @your-org, which removes a whole class of name collisions and makes it obvious to readers which packages come from the same maintainers.

2. Naming and ownership

The npm Docs set the rules: names are lowercase, URL-safe, cannot start with a dot or underscore, and the whole name including the scope has a length limit. A user scope matches your username; an organisation scope matches the org name. Pick the org name with the same care as a domain, because renaming it later means renaming every package.

3. The first-publish access trap

Scoped packages are private by default. Publishing a new public scoped package without saying so fails, or would require a paid private plan. Make the intent explicit in the package itself so CI and humans behave the same way:

{
  "name": "@example-org/package-n",
  "version": "1.0.0",
  "publishConfig": {
    "access": "public"
  }
}

The equivalent one-off command is npm publish --access public. After the first publish, the access level sticks.

4. Private registries and dependency confusion

Teams often host internal packages on a private registry. If those packages are unscoped, a package manager configured with both registries may fetch a public package of the same name instead, a pattern widely described as dependency confusion. Scoping internal packages, and mapping the scope to your registry in .npmrc, closes that gap:

@example-org:registry=https://registry.internal.example/
//registry.internal.example/:_authToken=${INTERNAL_NPM_TOKEN}

Consider also claiming the same scope on the public registry, even if you never publish there, so nobody else can.

5. Moving an existing package

A scoped name is a different package as far as npm is concerned. Existing users will not migrate on their own. The pattern that works: publish the new scoped package, release a final version of the old one that re-exports it or explains the move, then run npm deprecate on the old name with a message pointing to the new one. Do not unpublish the old name; it breaks installs and frees it for someone else.

Questions from the room

Do scopes help with typosquatting?

Partly. Someone can still register @exampel-org. But users only need to verify one scope name rather than every package name, which is a much smaller surface.

Can two scopes publish the same package name?

Yes. @alpha/logger and @beta/logger are unrelated packages. Inside your code, the import path includes the scope, so there is no clash.

Should every package in a monorepo share one scope?

Usually, yes. It signals they belong together and lets one .npmrc line cover them all. Our monorepo release notes continue from here.

Does the scope affect semver?

No. Versions are per package. Changing a package's name, scoped or not, is effectively starting over, so treat it like a major event in your changelog.

Take-home checklist

Related pages

An independent site with articles and free browser tools about building, evaluating, licensing and pricing reusable code modules. Informational only; nothing is sold on this site. Disclaimer