The name field in package.json is the unique identifier for your package. It's crucial for publishing to registries like npmjs.com and for other projects to correctly reference your package as a dependency.
Here are the rules and conventions for the name field:
Core Rules (Enforced by npm)
-
Lowercase: The name must be all lowercase letters.
- Invalid:
MyPackage, ReactDatepicker
- Valid:
mypackage, react-datepicker
-
Allowed Characters: Can contain lowercase alphanumeric characters (a-z, 0-9) and hyphens (-).
- Valid:
my-package, react-18-utils, lodash
-
Hyphen Restrictions: Cannot start or end with a hyphen.
- Invalid:
-my-package, my-package-
- Valid:
my-package
-
No Underscores or Spaces: Underscores (_) and spaces are not allowed.
- Invalid:
my_package, my package
- Valid:
my-package
-
No Special Characters (Mostly): Generally, avoid other special characters like ~, !, *, ', (, ), ,, {, }, [, ], |, :, ;, <, >, /, ?, #, @, . The allowed characters are quite restricted.
- Invalid:
my/package, my~package, my.package (dots are generally disallowed too, though some older packages might exist).
-
Length Limit: The name must be less than or equal to 214 characters.
-
Reserved Names:
- Cannot be reserved words or Node.js built-in module names (e.g.,
fs, http, path, node).
- Cannot be names that are already taken on the npm registry if you intend to publish.
- Cannot be just
., .., or _.
-
Scoped Names: You can use scoped names for organization, which look like @scope/package-name.
- The
scope part must be a valid npm username or organization name.
- The
package-name part must follow all the rules above (lowercase, alphanumeric, hyphens, etc.).
- Example:
@myorg/ui-components, @username/my-private-package
Conventions & Best Practices
- Uniqueness: If you plan to publish your package to npm, the name must be globally unique across the entire npm registry. If it's just for a local project or internal use, uniqueness is only required within your project's dependencies.
- Descriptive and Concise: Choose a name that clearly indicates what the package does. Keep it as short as possible while remaining descriptive.
- Avoid Hyphens if Simple: For very simple packages,
lodash or react are clear. Hyphens (my-utility-function) are often used to separate words and improve readability.
- Consider Scopes for Personal/Team Projects: Using scoped names (
@your-username/your-package) is a good practice for personal projects or team projects hosted on npm. It helps prevent naming conflicts and clearly associates the package with a specific user or organization.
- Check for Availability: Before settling on a name you intend to publish, search npmjs.com to ensure it's not already taken.
Example package.json snippet:
{
"name": "my-awesome-package", // Valid name
"version": "1.0.0",
"description": "A short description of my package.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
If you are working on a project within an organization and want to use a name that might conflict or just for organizational purposes, you might use a scoped name:
{
"name": "@my-company/internal-tool", // Valid scoped name
"version": "1.0.0",
// ... other fields
}