When you need to switch back from using pnpm to npm, you can achieve this by following these steps:
1. Clean up existing dependencies and lock files
Since pnpm uses the pnpm-lock.yaml file while npm uses the package-lock.json file, you first need to prune the existing dependencies and remove the lock files. Execute in the project root directory:
bashpnpm store prune rm -rf node_modules pnpm-lock.yaml
2. Initialize npm lock files and node modules
Next, install dependencies using npm, which will create the node_modules directory and package-lock.json file.
bashnpm install
This command installs dependencies based on the dependencies specified in package.json and creates a new package-lock.json lock file.
3. Verify the project
After installing dependencies, ensure the application or project runs correctly:
bashnpm run test # Run tests to verify project functionality
Or run the project to ensure all functionalities work correctly:
bashnpm start # This depends on the specific start script configuration
4. Commit changes to the version control system
If you are using version control (e.g., git), remember to commit changes. This includes the deleted pnpm-lock.yaml and newly added package-lock.json, as well as possible updates to node_modules:
bashgit add package-lock.json git rm pnpm-lock.yaml git commit -m "Switch from pnpm to npm" git push
Example
Suppose you previously used pnpm to manage a Node.js project, and now due to certain compatibility or team policy reasons, you need to switch back to npm. Follow the steps above: first prune the pnpm lock files and node modules, then use npm install to recreate the npm lock file and download dependencies. After completing these steps, run tests in the project to ensure everything functions correctly, and finally commit these changes to the version control system.
This process ensures a smooth transition from pnpm to npm while maintaining project stability and dependency consistency.