npm Unexpected Token '.' Error: Causes and Solutions
Problem Statement
When trying to install packages with npm install
(specifically npm install -g @angular/cli
in this case), users encounter the error:
npm ERR! Unexpected token '.'
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\<user>\AppData\Local\npm-cache\_logs\2022-02-02T15_25_07_320Z-debug-0.log
This error typically occurs with npm version 8.3.1 and Node.js version 17.4.0 or later, particularly on Windows systems using Node Version Manager (nvm).
Root Cause
The "Unexpected token '.'" error is a known issue with npm 8.3.1 and certain Node.js versions. The problem stems from compatibility issues between:
- npm versions (particularly 8.3.1)
- Node.js versions (especially 16.14.0+ and 17.x)
- nvm-windows versions (especially older releases)
Solutions
1. Update Node Version Manager (nvm)
WARNING
If you're using nvm-windows, ensure you have the latest version installed.
The most common solution is to update nvm-windows to version 1.1.9 or later:
- Download the latest nvm-update.zip from the official releases page
- Extract and run the installer
- After updating, remove and reinstall your Node.js version:
nvm uninstall 18.X.X # Replace with your current version
nvm install 18.X.X # Reinstall the same version
nvm use 18.X.X # Switch to the reinstalled version
2. Use a Compatible Node.js Version
TIP
Node.js 16.13.2 with npm 8.1.2 is known to work well with Angular CLI installations.
# Install a compatible Node.js version
nvm install 16.13.2
nvm use 16.13.2
# Now install Angular CLI
npm install -g @angular/cli
3. Update npm to a Stable Version
If you can run npm commands with a working version, update to a stable npm release:
npm install -g npm@latest
4. Alternative Package Manager
As a temporary workaround, you can use Yarn instead of npm:
yarn global add @angular/cli
5. Chocolatey Installation (if using choco)
If you installed nvm via Chocolatey:
choco uninstall nvm
choco install nvm
nvm install latest
nvm use [latest-version]
Prevention Strategies
To avoid similar issues in the future:
- Keep nvm updated: Regularly check for nvm-windows updates
- Verify compatibility: Check Angular CLI documentation for supported Node.js versions before installation
- Use LTS versions: Prefer Node.js Long-Term Support (LTS) versions for better stability
- Test new versions: Try new Node.js/npm versions in a isolated environment before switching projects
Debugging Steps
If you continue to experience issues:
- Check the detailed error log at the path shown in the error message
- Verify your nvm version with
nvm --version
- Confirm your Node.js and npm versions with
node --version
andnpm --version
- Ensure you've run
nvm use
after installing a new Node.js version
INFO
The "Unexpected token '.'" error is typically resolved by ensuring compatibility between your npm version, Node.js version, and nvm implementation. The solutions above address the most common compatibility issues reported by the community.
By following these steps, you should be able to resolve the npm "Unexpected token '.'" error and successfully install Angular CLI or other npm packages.