Node.js virtual environment builder for creating isolated Node.js environments
80
npm installation and package management functionality including npm setup, requirements file processing, and package installation with version management.
Installs npm package manager in the Node.js virtual environment with platform-specific optimizations.
def install_npm(env_dir, _src_dir, args):
"""
Install npm package manager in Node.js environment.
Parameters:
env_dir (str): Environment directory path
_src_dir (str): Source directory (unused in current implementation)
args (argparse.Namespace): Configuration arguments
Installs npm using the version specified in args.npm:
- 'latest' for most recent npm version
- Specific version number (e.g., '8.19.0')
- Downloads and configures npm for the environment
- Sets up proper npm configuration and cache directories
"""Platform-specific npm installation function optimized for Windows environments.
def install_npm_win(env_dir, src_dir, args):
"""
Install npm on Windows platforms with Windows-specific handling.
Parameters:
env_dir (str): Environment directory path
src_dir (str): Source/download directory
args (argparse.Namespace): Configuration arguments
Handles Windows-specific npm installation requirements:
- Windows path separators and executable extensions
- npm.cmd and npm batch file setup
- Windows registry and environment variable configuration
- PowerShell execution policy considerations
"""Installs npm packages from requirements files, similar to pip's requirements.txt functionality.
def install_packages(env_dir, args):
"""
Install npm packages from requirements file.
Parameters:
env_dir (str): Environment directory path
args (argparse.Namespace): Configuration arguments containing requirements file path
Processes requirements file containing package specifications:
- Package names with specific versions (e.g., 'express@4.18.0')
- Package names with version ranges
- Git repository URLs
- Local file paths
Requirements file format example:
express@4.18.0
lodash@^4.17.21
moment@>=2.29.0
Installs packages globally within the Node.js environment
and handles dependency resolution and version conflicts.
"""The activation scripts created by nodeenv include built-in package management functions:
The freeze function is available in activated environments and provides functionality similar to pip freeze.
# In bash/zsh environments
freeze [output_file]
# List installed packages
freeze
# Save to requirements file
freeze requirements.txt
# List local packages only
freeze -l requirements.txt# In fish shell environments
freeze [output_file]
# List installed packages
freeze
# Save to requirements file
freeze requirements.txt
# List local packages only
freeze -l requirements.txtThe freeze function:
-l flag--requirements optionimport nodeenv
from argparse import Namespace
args = Namespace(
npm='8.19.0', # Specific npm version
with_npm=True
)
nodeenv.install_npm('/path/to/env', '/tmp/src', args)import nodeenv
from argparse import Namespace
# Create requirements.txt with content:
# express@4.18.0
# lodash@4.17.21
# moment@2.29.4
args = Namespace(
requirements='/path/to/requirements.txt',
jobs='2'
)
nodeenv.install_packages('/path/to/env', args)import nodeenv
from argparse import Namespace
# Create environment and install packages
args = Namespace(
node='18.17.0',
npm='9.8.0',
with_npm=True,
requirements='/path/to/requirements.txt',
jobs='4'
)
env_dir = '/path/to/myenv'
src_dir = '/tmp/nodeenv-src'
# Create environment
nodeenv.create_environment(env_dir, args)
# Packages are automatically installed if requirements file specified# Activate environment
source myenv/bin/activate
# Install some packages
npm install -g express lodash moment
# List installed packages
freeze
# Output:
# express@4.18.2
# lodash@4.17.21
# moment@2.29.4
# Save to requirements file
freeze requirements.txt
# Deactivate environment
deactivate_nodeInstall with Tessl CLI
npx tessl i tessl/pypi-nodeenvdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10