A comprehensive Go SDK for npm operations with cross-platform support.
🌐 Complete Documentation Website
Visit our comprehensive documentation website for detailed guides, API references, and examples.
- Automatic npm Installation: Detect and install npm automatically based on your operating system
- Portable Version Support: Download and manage portable Node.js/npm versions
- Complete API Coverage: Full wrapper for all common npm commands
- Cross-Platform Support: Works on Windows, macOS, and Linux
- Project Management: Read, write, and manage package.json files
- High Performance: Asynchronous execution with timeout control
- Type Safety: Comprehensive error handling with structured error types
go get github.com/scagogogo/go-npm-sdk
package main
import (
"context"
"fmt"
"log"
"github.com/scagogogo/go-npm-sdk/pkg/npm"
)
func main() {
// Create npm client
client, err := npm.NewClient()
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// Check if npm is available
if !client.IsAvailable(ctx) {
// Auto-install npm
if err := client.Install(ctx); err != nil {
log.Fatal(err)
}
}
// Get npm version
version, err := client.Version(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("npm version: %s\n", version)
// Install a package
err = client.InstallPackage(ctx, "lodash", npm.InstallOptions{
SaveDev: false,
SaveExact: true,
})
if err != nil {
log.Fatal(err)
}
fmt.Println("Package installed successfully!")
}
💡 Need more examples? Check out our comprehensive documentation for detailed guides and advanced usage patterns.
The SDK can automatically detect and install npm based on your operating system:
client, _ := npm.NewClient()
ctx := context.Background()
if !client.IsAvailable(ctx) {
// Automatically install npm using the best method for your OS
err := client.Install(ctx)
if err != nil {
log.Fatal(err)
}
}
Download and manage portable Node.js/npm versions without system-wide installation:
manager, err := npm.NewPortableManager("/opt/npm-portable")
if err != nil {
log.Fatal(err)
}
// Install Node.js 18.17.0 with npm
config, err := manager.Install(ctx, "18.17.0")
if err != nil {
log.Fatal(err)
}
// Create client for this version
client, err := manager.CreateClient("18.17.0")
if err != nil {
log.Fatal(err)
}
Read, write, and manage package.json files:
pkg := npm.NewPackageJSON("./package.json")
// Load existing package.json
err := pkg.Load()
if err != nil {
log.Fatal(err)
}
// Modify package information
pkg.SetName("my-package")
pkg.SetVersion("2.0.0")
pkg.AddDependency("lodash", "^4.17.21")
pkg.AddScript("build", "webpack")
// Save changes
err = pkg.Save()
if err != nil {
log.Fatal(err)
}
Detect the current platform for platform-specific operations:
import "github.com/scagogogo/go-npm-sdk/pkg/platform"
detector := platform.NewDetector()
info, err := detector.Detect()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Platform: %s\n", info.Platform)
fmt.Printf("Architecture: %s\n", info.Architecture)
if info.IsLinux() {
fmt.Printf("Linux distribution: %s\n", info.Distribution)
}
For complete API documentation, visit our documentation website:
📚 Complete Documentation Website
The documentation includes:
- Complete API reference
- Usage guides and tutorials
- Examples and best practices
- Platform-specific information
See the examples directory for more comprehensive examples:
- Basic Usage - Getting started with the SDK
- Package Management - Installing and managing packages
- Portable Installation - Using portable npm versions
- Platform Detection - Detecting platform information
- Dependency Management - Managing dependencies
- Windows: Windows 10/11, Windows Server 2019/2022
- macOS: macOS 10.15+ (Intel and Apple Silicon)
- Linux: Ubuntu, Debian, CentOS, RHEL, Fedora, SUSE, Arch, Alpine
The SDK supports multiple npm installation methods:
- Package Manager: Use system package managers (apt, yum, brew, etc.)
- Official Installer: Download and run official Node.js installer
- Portable: Download portable Node.js/npm version
- Manual: Manual installation guidance
- Go 1.19 or later
- Internet connection for downloading npm/Node.js (if not already installed)
We welcome contributions! Please see our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
- GitHub Issues - Report bugs and request features
- GitHub Discussions - Ask questions and share ideas
- 📖 Documentation Website - Complete documentation and guides