In this article, we analyze how create-next-app validates your project name.
validate: (name) => {
const validation = validateNpmName(basename(resolve(name)))
if (validation.valid) {
return true
}
return 'Invalid project name: ' + validation.problems[0]
},
Have you tried naming your project with spaces in it when using create-next-app command? if you have done so, it won’t allow spaces in your project because it follows certain principles when it comes to naming your project.
So what are these naming convention rules?
validateNpmName function
If you check this create-next-app/index.ts, it calls a function named validateNpmName
. This is imported from helpers/validate-pkg.ts
This function is straight forward, calls a function named validateProjectName
that is imported from validate-npm-package-name.
Documentation says that if a name is valid, you will get the below object back:
{
validForNewPackages: true,
validForOldPackages: true
}
What makes a name valid? let’s check the documentation again. Documentataion provides these Naming rules:
package name length should be greater than zero
all the characters in the package name must be lowercase i.e., no uppercase or mixed case names are allowed
package name can consist of hyphens
package name must not contain any non-url-safe characters (since name ends up being part of a URL)
package name should not start with . or _
package name should not contain any spaces
package name should not contain any of the following characters: ~)(‘!*
package name cannot be the same as a node.js/io.js core module nor a reserved/blacklisted name. For example, the following names are invalid:
— http
— stream
— node_modules
— favicon.icopackage name length cannot exceed 214
These are the rules you should keep in mind when naming your Next.js project.
About me:
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.
I am open to work on an interesting project. Send me an email at ramu.narasinga@gmail.com
My Github - https://github.com/ramu-narasinga
My website - https://ramunarasinga.com
My Youtube channel - https://www.youtube.com/@thinkthroo
Learning platform - https://thinkthroo.com
Codebase Architecture - https://app.thinkthroo.com/architecture
Best practices - https://app.thinkthroo.com/best-practices
Production-grade projects - https://app.thinkthroo.com/production-grade-projects
References:
1. https://github.com/vercel/next.js/blob/canary/packages/create-next-app/index.ts#L162
2. https://github.com/vercel/next.js/blob/canary/packages/create-next-app/helpers/validate-pkg.ts#L13
3. https://www.npmjs.com/package/validate-npm-package-name
4. https://github.com/npm/validate-npm-package-name/tree/main
Top comments (0)