Skip to main content

Logging Node Dependencies

Intro​

If you are working on a Node project in a group and you are getting "it works on my machine", but you don't want to Dockerize the project, you can output the dependencies to a file for debugging.

Final Script​

Here is the finalized shell script. We will get into some of the simpler commands below if you don't want to use this whole script.

#!/usr/bin/env bash
#
# generate_environment_report.sh
#
# A verbose shell script that:
# 1. Collects Node.js and system information
# 2. Captures global Node.js dependencies
# 3. Captures local project dependencies (if a package.json is found)
# 4. Combines them into a single JSON report
# 5. Writes the output to environment_report.json

################################################################################
# CONFIGURATION
################################################################################

OUTPUT_FILE="environment_report.json"
SYSTEM_INFO_TMP="$(mktemp)" # Temporary file to store system info

################################################################################
# HELPER FUNCTIONS
################################################################################

##
# Logs an informational message to stdout
# Usage: log_info "some message"
##
log_info() {
local msg="$1"
echo "[INFO] $msg"
}

##
# Logs a warning to stdout
# Usage: log_warn "some warning message"
##
log_warn() {
local msg="$1"
echo "[WARN] $msg"
}

##
# Logs an error message to stderr
# Usage: log_error "error message"
##
log_error() {
local msg="$1"
>&2 echo "[ERROR] $msg"
}

##
# safe_npm_list
#
# Runs `npm list ... --json` in a subshell, capturing stdout into a temporary
# file. Discards stderr to avoid mixing JSON with warnings. If the temporary
# file contains no data (npm may exit non-zero or produce empty output on errors),
# fallback to an empty JSON object ("{}").
#
# Usage:
# safe_npm_list [-g --depth=0]
# safe_npm_list --all
##
safe_npm_list() {
local tmpfile
tmpfile="$(mktemp)"

npm list "$@" --json 1>"$tmpfile" 2>/dev/null

if [[ -s "$tmpfile" ]]; then
# The tmpfile is non-empty, so presumably it contains valid JSON output.
cat "$tmpfile"
else
# Fallback to empty JSON if nothing was written
echo "{}"
fi

rm -f "$tmpfile"
}

################################################################################
# MAIN SCRIPT LOGIC
################################################################################

log_info "Collecting environment details..."

# 1) System information
log_info "Gathering Node.js and OS details..."
node_version="$(node -v 2>/dev/null)"
if [[ -z "$node_version" ]]; then
node_version="Not Found"
log_warn "Node.js not found or not in PATH."
fi

npm_version="$(npm -v 2>/dev/null)"
if [[ -z "$npm_version" ]]; then
npm_version="Not Found"
log_warn "NPM not found or not in PATH."
fi

os_info="$(uname -a)"

cat <<EOL > "$SYSTEM_INFO_TMP"
{
"node_version": "$node_version",
"npm_version": "$npm_version",
"os_info": "$os_info"
}
EOL

# 2) Global Node.js dependencies
log_info "Collecting global Node.js dependencies..."
global_dependencies="$(safe_npm_list -g --depth=0)"

# 3) Project dependencies
log_info "Checking for local project dependencies..."
if [[ -f "package.json" ]]; then
log_info "package.json found. Collecting project dependencies..."
project_dependencies="$(safe_npm_list --all)"
else
log_warn "No package.json found in the current directory. Skipping project dependencies."
project_dependencies="{}"
fi

# 4) Combine all data into the output file
log_info "Combining data into $OUTPUT_FILE..."
{
echo "{"
echo " \"system_info\": $(cat "$SYSTEM_INFO_TMP"),"
echo " \"global_dependencies\": $global_dependencies,"
echo " \"project_dependencies\": $project_dependencies"
echo "}"
} > "$OUTPUT_FILE"

# 5) Cleanup temporary files
log_info "Cleaning up temporary files..."
rm -f "$SYSTEM_INFO_TMP"

log_info "Environment report saved to '$OUTPUT_FILE'."

################################################################################
# END OF SCRIPT
################################################################################

To use the script follow these steps:

  1. Copy the script into a file named generate_environment_report.sh.

  2. Make the script executable with chmod +x generate_environment_report.sh.

  3. Run the script with ./generate_environment_report.sh.

With the output files, you can compare the dependencies between different machines.

Simpler Commands​

If you don't want to use the whole script, you can use these simpler commands to list the dependencies.

  1. Output the list of project dependencies and their versions
npm list --all --json > project-dependencies.json
  1. List global npm packages
npm list -g --depth=0 --json > global-node-dependencies.json
  1. Get system information
node -v > system-info.txt
npm -v >> system-info.txt
uname -a >> system-info.txt

Comments