jsrosetta

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 run

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

Reference: github.com/miguelmota/golang-for-nodejs-developers#modules