R/validate-node.R

Defines functions validate_node_npm

Documented in validate_node_npm

#' Validate Node.js and npm availability
#'
#' Checks for Node.js and npm, preferring locally installed versions
#' managed by shinyelectron over system installations, and stops with class
#' `shinyelectron_node_too_old` when Node.js is older than `NODE_MIN_VERSION`.
#'
#' @return Invisibly returns a list with node and npm paths and versions.
#' @keywords internal
validate_node_npm <- function() {
  node_cmd <- get_node_command(prefer_local = TRUE)
  npm_cmd <- get_npm_command(prefer_local = TRUE)
  using_local <- nodejs_is_installed()

  node_result <- run_command_safe(node_cmd, "--version")

  if (node_result$status != 0) {
    cli::cli_abort(c(
      "Node.js is required but not found",
      "i" = "Install locally with: {.code shinyelectron::install_nodejs()}",
      "i" = "Or install system-wide from https://nodejs.org/"
    ))
  }

  node_version <- trimws(gsub("^v", "", node_result$stdout))
  # A version this cannot read (a pre-release build, say) is let through.
  found <- numeric_version(node_version, strict = FALSE)
  if (!is.na(found) && found < NODE_MIN_VERSION) {
    hints <- c("i" = "Install a current version with: {.code shinyelectron::install_nodejs()}")
    # A local install is used before the system one, so updating the system
    # one helps only when there is no local install.
    if (!using_local) {
      hints <- c(hints, "i" = "Or update the system-wide install from https://nodejs.org/")
    }
    cli::cli_abort(c(
      "Node.js {node_version} is too old; {NODE_MIN_VERSION} or newer is required",
      hints
    ), class = "shinyelectron_node_too_old")
  }

  npm_result <- run_command_safe(
    npm_cmd, "--version",
    env = nodejs_subprocess_env()
  )

  if (npm_result$status != 0) {
    cli::cli_abort(c(
      "npm is required but not found",
      "i" = "npm should be installed with Node.js",
      "i" = "Try: {.code shinyelectron::install_nodejs()}"
    ))
  }

  invisible(list(
    node_path = node_cmd,
    node_version = node_version,
    npm_path = npm_cmd,
    npm_version = trimws(npm_result$stdout),
    using_local = using_local
  ))
}

Try the shinyelectron package in your browser

Any scripts or data that you put into this service are public.

shinyelectron documentation built on Dec. 3, 2026, 9:07 a.m.