Give me a string and I\textquotesingle{}ll tell you if it\textquotesingle{}s a valid {\ttfamily npm} package name.

This package exports a single synchronous function that takes a {\ttfamily string} as input and returns an object with two properties\+:


\begin{DoxyItemize}
\item {\ttfamily valid\+For\+New\+Packages} \+:: {\ttfamily Boolean}
\item {\ttfamily valid\+For\+Old\+Packages} \+:: {\ttfamily Boolean}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31751}{}\doxysection{Contents}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31751}

\begin{DoxyItemize}
\item \href{\#naming-rules}{\texttt{ Naming rules}}
\item \href{\#examples}{\texttt{ Examples}}
\begin{DoxyItemize}
\item \href{\#valid-names}{\texttt{ Valid Names}}
\item \href{\#invalid-names}{\texttt{ Invalid Names}}
\end{DoxyItemize}
\item \href{\#legacy-names}{\texttt{ Legacy Names}}
\item \href{\#tests}{\texttt{ Tests}}
\item \href{\#license}{\texttt{ License}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31752}{}\doxysection{Naming Rules}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31752}
Below is a list of rules that valid {\ttfamily npm} package name should conform to.


\begin{DoxyItemize}
\item package name length should be greater than zero
\item all the characters in the package name must be lowercase i.\+e., no uppercase or mixed case names are allowed
\item package name {\itshape can} consist of hyphens
\item package name must {\itshape not} contain any non-\/url-\/safe characters (since name ends up being part of a URL)
\item package name should not start with {\ttfamily .} or {\ttfamily \+\_\+}
\item package name should {\itshape not} contain any leading or trailing spaces
\item package name should {\itshape not} contain any of the following characters\+: `$\sim$)('!$\ast$\`{}
\item package name {\itshape cannot} be the same as a node.\+js/io.js core module nor a reserved/blacklisted name. For example, the following names are invalid\+:
\begin{DoxyItemize}
\item http
\item stream
\item node\+\_\+modules
\item favicon.\+ico
\end{DoxyItemize}
\item package name length cannot exceed 214
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31753}{}\doxysection{Examples}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31753}
\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31754}{}\doxysubsection{Valid Names}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31754}

\begin{DoxyCode}{0}
\DoxyCodeLine{var validate = require("{}validate-\/npm-\/package-\/name"{})}
\DoxyCodeLine{}
\DoxyCodeLine{validate("{}some-\/package"{})}
\DoxyCodeLine{validate("{}example.com"{})}
\DoxyCodeLine{validate("{}under\_score"{})}
\DoxyCodeLine{validate("{}123numeric"{})}
\DoxyCodeLine{validate("{}excited!"{})}
\DoxyCodeLine{validate("{}@npm/thingy"{})}
\DoxyCodeLine{validate("{}@jane/foo.js"{}) }

\end{DoxyCode}


All of the above names are valid, so you\textquotesingle{}ll get this object back\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  validForNewPackages: true,}
\DoxyCodeLine{  validForOldPackages: true}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31755}{}\doxysubsection{Invalid Names}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31755}

\begin{DoxyCode}{0}
\DoxyCodeLine{validate("{} leading-\/space:and:weirdchars"{}) }

\end{DoxyCode}


That was never a valid package name, so you get this\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  validForNewPackages: false,}
\DoxyCodeLine{  validForOldPackages: false,}
\DoxyCodeLine{  errors: [}
\DoxyCodeLine{    'name cannot contain leading or trailing spaces',}
\DoxyCodeLine{    'name can only contain URL-\/friendly characters'}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31756}{}\doxysection{Legacy Names}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31756}
In the old days of npm, package names were wild. They could have capital letters in them. They could be really long. They could be the name of an existing module in node core.

If you give this function a package name that {\bfseries{used to be valid}}, you\textquotesingle{}ll see a change in the value of {\ttfamily valid\+For\+New\+Packages} property, and a warnings array will be present\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{validate("{}eLaBorAtE-\/paCkAgE-\/with-\/mixed-\/case-\/and-\/more-\/than-\/214-\/characters-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/"{}) }

\end{DoxyCode}


returns\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  validForNewPackages: false,}
\DoxyCodeLine{  validForOldPackages: true,}
\DoxyCodeLine{  warnings: [}
\DoxyCodeLine{    "{}name can no longer contain capital letters"{},}
\DoxyCodeLine{    "{}name can no longer contain more than 214 characters"{}}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31757}{}\doxysection{Tests}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31757}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install}
\DoxyCodeLine{npm test }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31758}{}\doxysection{License}\label{md_sfcode_node_modules_validate_npm_package_name_README_autotoc_md31758}
ISC 