npm valid scopes not working on Windows ✋🏼
npm’s registry and CLI allow dots in scope names, but PowerShell on Windows fails to parse them unless the name is wrapped in (single) quotes. Despite this, the install command shown on npmjs.com omits the quotes, leading to immediate errors for Windows users who copy‑paste the official command. I do mitigate this by providing my own install command in the package’s README but it’s not optimal nor desired.
The problem involves all present and future packages with a scope name that contains a dot - not just mine.
Taken from one of my package’s npm page:
npm i @igor.dvlpr/astro-component # ❌ Fails in PowerShell
npm i '@igor.dvlpr/astro-component' # ✅ Works in PowerShell (and everywhere else)
To elaborate, following the official npm documentation which states:
A package.json file must contain “name” and “version” fields.
The “name” field contains your package’s name and must be lowercase without any spaces. May contain hyphens, dots, and underscores.
The same rules apply to scope name; thus, it can contain dots, hyphens and underscores.
Why it happens: When the npm-provided command:
npm i @igor.dvlpr/astro-component # ❌ Fails in PowerShell
gets executed in PowerShell - which is the default command line for Windows10+ shell - it will throw an exception:
The splatting operator '@' cannot be used to reference variables in an expression. '@igor' can be used only as an argument to a command. To reference variables in an expression use '$igor'.
More info on the official PowerShell splatting documentation.
The exception occurs because it tries to access a property, in this case, it tries to access the property dvlpr/astro-component
of the variable $igor
- neither of those exists, nor is valid. Also, notice the highlighting, in the first command $igor
is indeed recognized/highlighted as a variable.