Skip to main content
Uncategorized

Resolving npm Installation Issues Related to Python and distutils

By October 18, 2024No Comments

Problem

While trying to install npm packages for a Vue.js project, I encountered errors related to the 

1
deasync
 package and Python’s 
1
distutils
 module. The error message indicated that 
1
distutils
 was not found, despite having Python installed on my system.

Diagnosis

  1. The project was using an older version of Node.js (v16.17.1).
  2. The system Python (version 3.13) did not include the distutils module.
  3. npm was not correctly configured to use a Python version with distutils available.

Solution

The issue was resolved by following these steps:

  1. Update Node.js to the latest LTS version:bashCopy
    1
    nvm use v20.11.1
  2. Verify Python 3.11 installation (which includes 
    1
    distutils
    ):bashCopy
    1
    /opt/homebrew/opt/python@3.11/bin/python3.11 --version
  3. Confirm 
    1
    distutils
     availability in Python 3.11:bashCopy
    1
    /opt/homebrew/opt/python@3.11/bin/python3.11 -c "import distutils; print('distutils is available')"
  4. Set npm to use Python 3.11:bashCopy
    1
    export npm_config_python=/opt/homebrew/opt/python@3.11/bin/python3.11
  5. Clear npm cache and reinstall dependencies:bashCopy
    1
    npm cache clean --force rm -rf node_modules package-lock.json npm install

Key Takeaways

  1. Python Version Matters: Newer versions of Python (3.12+) have removed 
    1
    distutils
    , which some npm packages rely on for compilation.
  2. Node.js and npm Compatibility: Ensure you’re using a compatible Node.js version for your project and npm version.
  3. npm Configuration: Properly configuring npm to use the correct Python version can resolve many installation issues.

Additional Notes

  • Consider using a Python version manager like 
    1
    pyenv
     for easier management of multiple Python versions.
  • For projects requiring specific Python versions, consider creating a virtual environment.
  • Regularly update your development tools and be aware of major changes in new versions that might affect your projects.

Remember, while this solution worked for my specific setup, your mileage may vary depending on your system configuration and project requirements.