Standard library
Managing Modules
Node.js's npm compared to Go modules, Cargo (Rust), Swift Package Manager, and Maven/Gradle (Java): installing, updating, removing, and exporting/importing packages.
- Minimum versions
- Node.js ≥ 19Go ≥ 1.16Rust ≥ 1.85Swift ≥ 6.0Java ≥ 25
- Verified on
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.
Each ecosystem has a dependency manifest file (package.json, go.mod, Cargo.toml, Package.swift, pom.xml/build.gradle) and tooling to manage it. npm and crates.io (Rust) have a central registry; a Go module or Swift package is usually just a git repository — nothing to publish to a registry (Go fetches through the GOPROXY module proxy, SwiftPM fetches straight from a git URL). Java splits this into three separate layers: package is just a namespace in the language, dependencies are managed by Maven/Gradle through pom.xml/build.gradle, and JPMS (module-info.java, since Java 9) is yet another, independent encapsulation layer.
Installing, updating, and removing dependencies
| Task | npm (Node.js) | Go modules | Cargo (Rust) | Swift Package Manager | Maven (Java) |
|---|---|---|---|---|---|
| Initialize the dependency file | npm init |
go mod init github.com/you/yourmodule |
cargo new/cargo init |
swift package init |
mvn archetype:generate |
| Install a package | npm install uuid |
go get github.com/google/uuid@v1.6.0 |
cargo add uuid -F v4 |
swift package add-dependency <url> --from <ver> |
add a <dependency> to pom.xml |
| Install a CLI globally | npm install -g <pkg> |
go install pkg@latest |
cargo install <crate> |
— (no standard way; typically Homebrew/Mint) | — (typically SDKMAN/jbang) |
| Update to the latest version | npm install uuid@latest |
go get -u github.com/google/uuid |
cargo update -p uuid |
swift package update |
edit the version in pom.xml |
| Remove a package | npm uninstall uuid |
go get github.com/google/uuid@none |
cargo remove uuid |
remove the dependency from Package.swift |
remove the <dependency> from pom.xml |
| Prune unused dependencies | npm prune |
go mod tidy |
— (no standard command) | — (no standard command) | mvn dependency:analyze (reports only) |
| Publish | npm publish |
push code and tag a release on the git repository | cargo publish |
tag a release on git (Swift Package Index indexes it automatically) | mvn deploy (to Maven Central via Sonatype) |
Importing an external package
// importing a module
import { v4 as uuidv4 } from 'uuid'
const id = uuidv4()
console.log(id) // a random uuid, different every runpackage main
import (
"fmt"
// importing a module
"github.com/google/uuid"
)
func main() {
id := uuid.New()
fmt.Println(id) // a random uuid, different every run
}// Cargo.toml: uuid = { version = "1", features = ["v4"] }
// importing a module
use uuid::Uuid;
fn main() {
let id = Uuid::new_v4();
println!("{id}"); // a random uuid, different every run
}// importing a module
import Algorithms
let unique = [1, 2, 2, 3, 3, 3].uniqued() // swift-algorithms: std has nothing like this
print(Array(unique)) // [1, 2, 3]// importing a module
import org.apache.commons.lang3.StringUtils;
// Maven: org.apache.commons:commons-lang3:3.20.0
void main() {
IO.println(StringUtils.capitalize("bob")); // Bob
}Exporting your own module
// greeter.js — exporting a module
export function greet(name) {
console.log(`hello ${name}`)
}
// main.js — importing the module you just exported
import { greet } from './greeter.js'
greet('bob') // hello bob// greeter/greeter.go — exporting a module
package greeter
import "fmt"
// Greet prints a greeting to name
func Greet(name string) {
fmt.Printf("hello %s", name)
}
// main.go — importing the module you just exported
package main
import "github.com/you/yourmodule/greeter"
func main() {
greeter.Greet("bob") // hello bob
}// src/greeter.rs — exporting a module
/// Greet prints a greeting to name
pub fn greet(name: &str) {
print!("hello {name}");
}
// src/main.rs — importing the module you just exported
mod greeter;
use greeter::greet;
fn main() {
greet("bob"); // hello bob
}// Sources/greeter/Greeter.swift — exporting a module
public enum Greeter {
/// Greet prints a greeting to name
public static func greet(_ name: String) {
print("hello \(name)", terminator: "")
}
}
// Sources/app/main.swift — importing the module you just exported (same package, declared as a target dependency in Package.swift)
import greeter
Greeter.greet("bob") // hello bob// greeter/Greeter.java — exporting a module
package greeter;
public class Greeter {
/** Prints a greeting to name */
public static void greet(String name) {
System.out.print("hello " + name);
}
}
// Main.java — importing the module you just exported
import greeter.Greeter;
void main() {
Greeter.greet("bob"); // hello bob
}Reference: github.com/miguelmota/golang-for-nodejs-developers#modules