Subject: [bug#51838] [PATCH v6 05/41] guix: node-build-system: Add 'delete-dependencies' helper function.

From: Liliana Marie Prikler <[email-masked]>
Date: Thu, 06 Jan 2022 17:50:08 +0100

Hi Leo,

Am Mittwoch, dem 05.01.2022 um 15:02 -0500 schrieb Leo Famulari:
> On Wed, Jan 05, 2022 at 02:08:30PM -0500, Philip McGrath wrote:
> > I would consider it very regrettable if this patch series were to
> > continue to be blocked by stylistic considerations in the
> > implementation of
> > unexported helper functions.
> 
> Agreed.
> 
> Is there a concrete problem with these patches? Or will they work as
> specified for Guix packagers?
> 
> Let's remember that the primary goal of code review is to bring a
> contribution into the codebase.
I'm currently in the process of applying my own checklist, see
<https://issues.guix.gnu.org/issue/51838/raw/352>

Each of my fixup commits is a change on top of Philip's corresponding
commit (or at least I hope so, I haven't squashed them yet) and delete-
dependencies.patch shows the savings in the rest of the series.  I so
far successfully built node-irc and node-serialport with these changes
applied on top.

It'll take some time to check the rest of the toplevel packages and
thereafter I'll squash and cleanup commit messages.

So from my position, everything is about to be done.  Should I resend
this as v8 for a complete check that I'm not doing anything harmful or
should I go ahead and commit once I'm done?

Cheers
From 280dd782003075444037297a4a5dd5723ff1f459 Mon Sep 17 00:00:00 2001
From: Liliana Marie Prikler <[email-masked]>
Date: Thu, 6 Jan 2022 15:08:24 +0100
Subject: [PATCH 1/3] Fixup 03/41.

This commit adds several utility functions for non-destructive
transformation of the JSON representation used by (guix build json),
particularly for purely functional update of JSON objects.  They ought
to eventually be exported from their own module, but for now are kept
private to allow experimentation.

* guix/build/node-build-system.scm (assoc-ref*, jsobject-ref, alist-pop)
(alist-update, jsobject-update*, jsobject-union): New variables.
(with-atomic-json-file-replacement): New public variable.
(module-name, build, patch-dependencies): Use them.  Do not resort to
unsafe alist primitives from Guile core.
---
 guix/build/node-build-system.scm | 313 +++++++++----------------------
 1 file changed, 85 insertions(+), 228 deletions(-)

diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index 9967223b86..d695424e50 100644
--- a/guix/build/node-build-system.scm
+++ b/guix/build/node-build-system.scm
@@ -25,35 +25,12 @@ (define-module (guix build node-build-system)
   #:use-module (ice-9 ftw)
   #:use-module (ice-9 match)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-71)
   #:export (%standard-phases
             with-atomic-json-file-replacement
             delete-dependencies
             node-build))
 
-;; Commentary:
-;;
-;; Builder-side code of the standard Node/NPM package install procedure.
-;;
-;; Code:
-
-;;;
-;;; JSON utilities.
-;;;
-;;; The following procedures facilitate transforming JSON values using the
-;;; representation from (guix build json), particularly purely functional
-;;; update of JSON objects.  If we decide to make more of them public, we
-;;; might instead put them in their own file or, eventually, add them to
-;;; (guix build json).
-;;;
-;;; JSON objects with duplicate keys are not interoperable: see RFC 8259 § 4.
-;;; These procedures assume, but generally do not check, that JSON objects
-;;; given to them as arguments do not have duplicate keys.  As long as that
-;;; precondition is satisfied, they will produce JSON objects without
-;;; duplicate keys.  Procedures that operate on unwrapped assosciation lists
-;;; may do likewise, which should be considered before exporting them for
-;;; general use.
-;;;
-
 (define (with-atomic-json-file-replacement file proc)
   "Like 'with-atomic-file-replacement', but PROC is called with a single
 argument---the result of parsing FILE's contents as json---and should a value
@@ -62,205 +39,83 @@ (define (with-atomic-json-file-replacement file proc)
     (lambda (in out)
       (write-json (proc (read-json in)) out))))
 
-(define (jsobject-ref js key failure-result)
-  "Return the value assosciated with KEY in the json object JS.  If KEY is not
-found and FAILURE-RESULT is a procedure, it is called in tail position with
-zero arguments.  Otherwise, FAILURE-RESULT is returned."
-  ;; TODO: `failure-result` should be optional, but should the default
-  ;; `failure-result` be #f (like `assoc-ref`), a thunk raising an exception,
-  ;; '(@), or something else?  Keep it mandatory until we discuss and decide.
+(define* (assoc-ref* alist key #:optional default)
+  "Like assoc-ref, but return DEFAULT instead of #f if no value exists."
+  (match (assoc key alist)
+    (#f default)
+    ((_ . value) value)))
+
+(define* (jsobject-ref obj key #:optional default)
+  (match obj
+    (('@ . alist) (assoc-ref* alist key default))))
+
+(define* (alist-pop alist key #:optional (= equal?))
+  "Return two values, the first pair in ALIST with key KEY, and the other
+elements.  Equality calls are made as (= KEY ALISTCAR)."
+  (define (found? pair)
+    (= key (car pair)))
+
+  (let ((before after (break found? alist)))
+    (if (pair? after)
+        (values (car after) (append before (cdr after)))
+        (values #f before))))
+
+(define* (alist-update alist key proc #:optional default (= equal?))
+  "Return an association list like ALIST, but with KEY mapped to the result of
+PROC applied to the first value found under the comparison (= KEY ALISTCAR).
+If no such value exists, use DEFAULT instead.
+Unlike acons, this removes the previous association of KEY (assuming it is
+unique), but the result may still share storage with ALIST."
+  (let ((pair rest (alist-pop alist key =)))
+    (acons key
+           (proc (if (pair? pair)
+                     (cdr pair)
+                     default))
+           rest)))
+
+(define (jsobject-update* js . updates)
+  "Return a json object like JS, but with all UPDATES applied.  Each update
+is a list (KEY PROC [DEFAULT]), so that KEY is mapped to the result of
+PROC applied to the value found for it, or DEFAULT otherwise."
   (match js
     (('@ . alist)
-     (match (assoc key alist)
-       (#f
-        (if (procedure? failure-result)
-            (failure-result)
-            failure-result))
-       ((_ . value)
-        value)))))
-
-(define (alist-pop alist key)
-  "Return two values: the first pair in ALIST with the given KEY in its
-'car' (or #f, if no such pair exists) and an assosciation list like (and
-potentially sharing storage with) ALIST, but with no entry for KEY."
-  (match (assoc key alist)
-    ;; If key isn't present, we don't need to do any allocation
-    (#f
-     (values #f alist))
-    (found
-     (values found
-             ;; Because we have `found`, we can find it more
-             ;; efficiently this time with `eq?`. We avoid using
-             ;; `delq` because it would copy pairs in a shared
-             ;; tail. We assume a sufficiently smart compiler to
-             ;; handle "tail recursion modulo cons" (vid. e.g. Indiana
-             ;; University Technical Report No. 19, Friedman & Wise
-             ;; 1975) at least as efficiently as a hand-written
-             ;; tail-recursive implementation with an accumulator.
-             (let loop ((alist alist))
-               (match alist
-                 ;; We know that `found` is present,
-                 ;; so no need to check for '()
-                 ((this . alist)
-                  (if (eq? this found)
-                      alist
-                      (cons this (loop alist))))))))))
-
-;; Sadly, Guile's implementation of (@ (srfi srfi-1) alist-delete)
-;; performs unnecessary allocation, e.g. this currently evaluates to #f:
-;;
-;;     (let ((alist `(("a" . 1)("b" . 2)("c" . 3))))
-;;       (eq? alist (alist-delete "x" alist)))
-;;
-;; These functions generally choose to allocate a new outer pair (with the '@
-;; tag), even though in unusual cases the resulting object might not have
-;; changed, for the sake of simplicity and to avoid retaining a reference to
-;; the original alist longer than necessary. But that is O(1) allocation that
-;; could only rarely be avoided: `alist-delete` would allocate O(n) pairs,
-;; which would only be necessary in the worst case.
-(define (alist-delete* alist key)
-  "Return an assosciation list like (and potentially sharing storage with)
-ALIST, but with no entry for KEY."
-  (define-values (_popped remaining)
-    (alist-pop alist key))
-  remaining)
-
-(define (jsobject-delete js key)
-  "Return a json object like JS, but with no entry for KEY."
-  (cons '@ (match js
-             (('@ . alist)
-              (alist-delete* alist key)))))
-
-(define (alist-set alist key value)
-  "Return an assosciation list like ALIST, but with KEY mapped to VALUE,
-replacing any existing mapping for KEY."
-  (acons key value (alist-delete* alist key)))
-
-(define (jsobject-set js key value)
-  "Return a json object like JS, but with KEY mapped to VALUE, replacing any
-existing mapping for KEY."
-  (cons '@ (match js
-             (('@ . alist)
-              (alist-set alist key value)))))
-
-(define jsobject-set*
-  (case-lambda
-    "Return a json object like JS, but functionally extended by mapping each
-KEY to each VALUE, replacing any existing mapping for each KEY.  The update
-takes place from left to right, so later mappings overwrite earlier mappings
-for the same KEY."
-    ((js)
-     js)
-    ((js key value)
-     (jsobject-set js key value))
-    ((js . args)
-     (cons '@ (match js
-                (('@ . alist)
-                 (let loop ((alist alist)
-                            (args args))
-                   (match args
-                     (()
-                     alist)
-                     ((key value . args)
-                      (loop (alist-set alist key value)
-                            args))))))))))
-
-(define (alist-update alist key failure-result updater)
-  "Return an assosciation list like ALIST, but with KEY mapped to the result
-of applying UPDATER to the value to which KEY is mapped in ALIST.  When ALIST
-does not have an existing mapping for KEY, FAILURE-RESULT is used as with
-'jsobject-ref' to obtain the argument for UPDATER."
-  ;; Often, `updater` will be a lambda expression, so making it the last
-  ;; argument may help to makes the code legible, and the most likely
-  ;; `failure-result` arguments are all shorter than the keyword
-  ;; `#:failure-result`.  Plus, making `failure-result` mandatory helps make
-  ;; `alist-update` consistent with `alist-update*`.
-  (define-values (popped tail-alist)
-    (alist-pop alist key))
-  (acons key
-         (updater (match popped
-                    (#f
-                     (if (procedure? failure-result)
-                         (failure-result)
-                         failure-result))
-                    ((_ . value)
-                     value)))
-         tail-alist))
-
-(define (jsobject-update js key failure-result updater)
-  "Return a json object like JS, but with KEY mapped to the result of applying
-UPDATER to the value to which KEY is mapped in JS.  When JS does not have an
-existing mapping for KEY, FAILURE-RESULT is used as with 'jsobject-ref' to
-obtain the argument for UPDATER."
-  (cons '@ (match js
-             (('@ . alist)
-              (alist-update alist key failure-result updater)))))
-
-(define jsobject-update*
-  (case-lambda
-    "Return a json object like JS, but functionally extended by replacing the
-mapping for each KEY with the result of applying the corresponding UPDATER to
-the value to which that KEY is mapped in JS---or, if no such mapping exists,
-to a value based on the corresponding FAILURE-RESULT as with 'jsobject-ref'.
-The update takes place from left to right, so later UPDATERs will receive the
-values returned by earlier UPDATERs for the same KEY."
-    ((js)
-     js)
-    ((js key failure-result updater)
-     (jsobject-update js key failure-result updater))
-    ((js . args)
-     (cons '@ (match js
-                (('@ . alist)
-                 (let loop ((alist alist)
-                            (args args))
-                   (match args
-                     (()
-                     alist)
-                     ((key failure-result updater . args)
-                      (loop (alist-update alist key failure-result updater)
-                            args))))))))))
-
-(define* (jsobject-union #:key
-                         (combine (lambda (a b) b))
-                         (combine/key (lambda (k a b) (combine a b)))
-                         #:rest json-objects)
-  "Combine the given JSON-OBJECTS into a single json object.  The JSON-OBJECTS
-are merged from left to right by adding each key/value pair of each object to
-the aggregate object in turn.  When one of the JSON-OBJECTS contains a mapping
-from some key KEY to a value VAL such that the aggregate object already
-contains a mapping from KEY to a value VAL0, the aggregate object is
-functionally updated to instead map KEY to the value of (COMBINE/KEY KEY VAL0
-VAL).  The default COMBINE/KEY tail-calls (COMBINE VAL0 VAL), and the default
-COMBINE simply returns its second argument, so, by default, mappings in later
-JSON-OBJECTS supersede those in earlier ones."
-  (match (filter (lambda (v)
-                   (not (or (keyword? v)
-                            (procedure? v))))
-                 json-objects)
-    (()
-     '(@))
-    (((and js0 ('@ . _)))
-     js0)
-    ((('@ . alist0) ('@ . alist*) ...)
-     (cons '@ (fold (lambda (alist1 alist0)
-                      (if (null? alist0)
-                          alist1
-                          (fold (lambda (k+v alist0)
-                                  (match k+v
-                                    ((k . v)
-                                     (define-values (popped tail-alist)
-                                       (alist-pop alist0 k))
-                                     (match popped
-                                       (#f
-                                        (cons k+v tail-alist))
-                                       ((_ . v0)
-                                        (acons k
-                                               (combine/key k v0 v)
-                                               tail-alist))))))
-                                alist0
-                                alist1)))
-                    alist0
-                    alist*)))))
+     (let loop ((alist alist)
+                (updates updates))
+       (match updates
+         (() (cons '@ alist))
+         (((key proc) . updates)
+          (loop (alist-update alist key proc #f equal?) updates))
+         (((key proc default) . updates)
+          (loop (alist-update alist key proc default equal?) updates)))))))
+
+(define (jsobject-union combine seed . objects)
+  "Merge OBJECTS into SEED by applying (COMBINE KEY VAL0 VAL), where VAL0
+is the value found in the (possibly updated) SEED and VAL is the new value
+found in one of the OBJECTS."
+  (match seed
+    (('@ . aseed)
+     (match objects
+       (() seed)
+       ((('@ . alists) ...)
+        (cons
+         '@
+         (fold (lambda (alist aseed)
+                 (if (null? aseed) alist
+                     (fold
+                      (match-lambda*
+                        (((k . v) aseed)
+                         (let ((pair tail (alist-pop alist k)))
+                           (match pair
+                             (#f (acons k v aseed))
+                             ((_ . v0) (acons k (combine k v0 v) aseed))))))
+                      aseed
+                      alist)))
+               aseed
+               alists)))))))
+
+;; Possibly useful helper functions:
+;; (define (newest key val0 val) val)
+;; (define (unkeyed->keyed proc) (lambda (_key val0 val) (proc val0 val)))
 
 
 ;;;
@@ -282,7 +137,7 @@ (define (set-home . _)
 (define (module-name module)
   (let* ((package.json (string-append module "/package.json"))
          (package-meta (call-with-input-file package.json read-json)))
-    (jsobject-ref package-meta "name" #f)))
+    (jsobject-ref package-meta "name")))
 
 (define (index-modules input-paths)
   (define (list-modules directory)
@@ -318,12 +173,14 @@ (define resolve-dependencies
     (lambda (pkg-meta)
       (jsobject-update*
        pkg-meta
-       "devDependencies" '(@) resolve-dependencies
-       "dependencies" '(@) (lambda (deps)
-                             (resolve-dependencies
-                              (jsobject-union
-                               (jsobject-ref pkg-meta "peerDependencies" '(@))
-                               deps))))))
+       `("devDependencies" ,resolve-dependencies (@))
+       `("dependencies" ,(lambda (deps)
+                           (resolve-dependencies
+                            (jsobject-union
+                             (lambda (k a b) b)
+                             (jsobject-ref pkg-meta "peerDependencies" '(@))
+                             deps)))
+         (@)))))
   #t)
 
 (define (delete-dependencies pkg-meta absent-dependencies)
-- 
2.34.0

From 41819ee4536ca36bfcb8e87b29503cf1791b056f Mon Sep 17 00:00:00 2001
From: Liliana Marie Prikler <[email-masked]>
Date: Thu, 6 Jan 2022 15:31:48 +0100
Subject: [PATCH 2/3] Fixup 04/41.

Packages with native addons currently try to write to store paths
when used as dependecies.  This patch adds a phase to replace that
behaviour with a no-op.

* guix/build/node-build-system.scm (avoid-node-gyp-rebuild): New
variable.
(%standard-phases): Add 'avoid-node-gyp-rebuild' after 'install'.

Signed-off-by: Liliana Marie Prikler <[email-masked]>
---
 guix/build/node-build-system.scm | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index d695424e50..1a0e187028 100644
--- a/guix/build/node-build-system.scm
+++ b/guix/build/node-build-system.scm
@@ -285,12 +285,16 @@ (define installed-package.json
     (search-input-file outputs (string-append "/lib/node_modules/"
                                               (module-name ".")
                                               "/package.json")))
-  ;; not with-atomic-json-file-replacement, because we usually don't
-  ;; want or need to overwrite it
+  ;; We don't want to use an atomic replacement here, because we often don't
+  ;; even need to overwrite this file.  Therefore, let's use some helpers
+  ;; that we'd otherwise not need.
   (define pkg-meta
     (call-with-input-file installed-package.json read-json))
   (define scripts
     (jsobject-ref pkg-meta "scripts" '(@)))
+  (define (jsobject-set js key val)
+    (jsobject-update* js (list key (const val))))
+
   (when (equal? "node-gyp rebuild" (jsobject-ref scripts "install" #f))
     (call-with-output-file installed-package.json
       (lambda (out)
-- 
2.34.0

From 0b839bf89df5188447275f960e9bbff3935afea4 Mon Sep 17 00:00:00 2001
From: Liliana Marie Prikler <[email-masked]>
Date: Thu, 6 Jan 2022 15:35:35 +0100
Subject: [PATCH 3/3] Fixup 05/41.

Many node packages currently skip the configure phase, because they lack
both dependencies and a convenient way to build without all of them, e.g.
for the purposes of bootstrapping.  This patch adds a big hammer to flatten
these nails.

* guix/build/node-build-system.scm (delete-dependencies): New variable.

Signed-off-by: Liliana Marie Prikler <[email-masked]>
---
 guix/build/node-build-system.scm | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/guix/build/node-build-system.scm b/guix/build/node-build-system.scm
index 1a0e187028..76aa51c52f 100644
--- a/guix/build/node-build-system.scm
+++ b/guix/build/node-build-system.scm
@@ -23,6 +23,7 @@ (define-module (guix build node-build-system)
   #:use-module (guix build utils)
   #:use-module (guix build json)
   #:use-module (ice-9 ftw)
+  #:use-module (ice-9 regex)
   #:use-module (ice-9 match)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-71)
@@ -183,22 +184,29 @@ (define resolve-dependencies
          (@)))))
   #t)
 
-(define (delete-dependencies pkg-meta absent-dependencies)
-  "Functionally update PKG-META, a json object corresponding to a
-'package.json' file, to allow building without the ABSENT-DEPENDENCIES.  To
-avoid reintroducing the ABSENT-DEPENDENCIES, only use this procedure after the
-'patch-dependencies' phase."
-  (define delete-fom-jsobject
+(define (delete-dependencies unwanted)
+  "Rewrite package.json to remove UNWANTED dependencies.  Unwanted dependencies
+can be specified as regular expressions to exclude a range of related
+dependencies, e.g. \"tap\" excludes all dependencies including \"tap\" in their
+name, but \"^tap$\" explicitly excludes \"tap\"."
+  (define delete-from-jsobject
     (match-lambda
       (('@ . alist)
        (cons '@ (filter (match-lambda
                           ((k . v)
-                           (not (member k absent-dependencies))))
+                           (not (any
+                                 (lambda (pattern)
+                                   (string-match pattern k))
+                                 unwanted))))
                         alist)))))
-  (jsobject-update*
-   pkg-meta
-   "devDependencies" '(@) delete-fom-jsobject
-   "dependencies" '(@) delete-fom-jsobject))
+
+  (with-atomic-json-file-replacement "package.json"
+    (lambda (pkg-meta)
+      (jsobject-update*
+       pkg-meta
+       `("peerDependencies" ,delete-from-jsobject (@))
+       `("devDependencies" ,delete-from-jsobject (@))
+       `("dependencies" ,delete-from-jsobject (@))))))
 
 (define* (delete-lockfiles #:key inputs #:allow-other-keys)
   "Delete 'package-lock.json', 'yarn.lock', and 'npm-shrinkwrap.json', if they
-- 
2.34.0

diff --git a/gnu/packages/node-xyz.scm b/gnu/packages/node-xyz.scm
index 1f51c0d636..990b26a689 100644
--- a/gnu/packages/node-xyz.scm
+++ b/gnu/packages/node-xyz.scm
@@ -297,9 +297,7 @@ (define-public node-semver
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta '("tap")))))))
+             (delete-dependencies '("^tap$")))))
        ;; FIXME: Tests depend on node-tap
        #:tests? #f))
     (home-page "https://github.com/npm/node-semver")
@@ -329,9 +327,7 @@ (define-public node-wrappy
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta '("tap")))))))))
+             (delete-dependencies '("^tap$")))))))
     (home-page "https://github.com/npm/wrappy")
     (synopsis "Callback wrapping utility")
     (description "@code{wrappy} is a utility for Node.js to wrap callbacks.")
@@ -356,9 +352,7 @@ (define-public node-once
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta '("tap")))))))
+             (delete-dependencies '("^tap$")))))
        ;; FIXME: Tests depend on node-tap
        #:tests? #f))
     (inputs
@@ -391,9 +385,7 @@ (define-public node-inherits
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta '("tap")))))))
+             (delete-dependencies '("^tap$")))))
        ;; FIXME: Tests depend on node-tap
        #:tests? #f))
     (home-page
@@ -425,10 +417,7 @@ (define-public node-safe-buffer
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta  '("tape"
-                                                  "standard")))))))
+             (delete-dependencies '("^tape$" "^standard$")))))
        #:tests? #f))
     (home-page
      "https://github.com/feross/safe-buffer")
@@ -458,12 +447,8 @@ (define-public node-string-decoder
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      '("tap"
-                                        "core-util-is"
-                                        "babel-polyfill")))))))
+             (delete-dependencies
+              '("^tap$" "^core-util-is$" "^babel-polyfill$")))))
        ;; FIXME: Tests depend on node-tap
        #:tests? #f))
     (inputs
@@ -497,29 +482,22 @@ (define-public node-readable-stream
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `("@babel/cli"
-                                        "@babel/core"
-                                        "@babel/polyfill"
-                                        "@babel/preset-env"
-                                        "airtap"
-                                        "assert"
-                                        "bl"
-                                        "deep-strict-equal"
-                                        "events.once"
-                                        "glob"
-                                        "gunzip-maybe"
-                                        "hyperquest"
-                                        "lolex"
-                                        "nyc"
-                                        "pump"
-                                        "rimraf"
-                                        "tap"
-                                        "tape"
-                                        "tar-fs"
-                                        "util-promisify")))))))
+             (delete-dependencies `("^@babel/"
+                                    "^airtap$"
+                                    "^assert$"
+                                    "^bl$"
+                                    "^deep-strict-equal$"
+                                    "^events\\.once$"
+                                    "^glob$"
+                                    "^gunzip-maybe$"
+                                    "^hyperquest$"
+                                    "^lolex$"
+                                    "^nyc$"
+                                    "^pump$"
+                                    "^rimraf$"
+                                    "^tape?$"
+                                    "^tar-fs$"
+                                    "^util-promisify$")))))
        #:tests? #f))
     (inputs
      (list node-util-deprecate node-string-decoder node-inherits))
@@ -554,10 +532,7 @@ (define-public node-irc-colors
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("istanbul"
-                                                 "vows")))))))
+             (delete-dependencies `("^istanbul$" "^vows$")))))
        #:tests? #f))
     (home-page "https://github.com/fent/irc-colors.js")
     (synopsis "Node.js module providing color and formatting for IRC")
@@ -586,12 +561,8 @@ (define-public node-irc
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("ansi-color"
-                                                 "faucet"
-                                                 "jscs"
-                                                 "tape")))))))
+             (delete-dependencies
+              `("^ansi-color$" "^faucet$" "^jscs$" "^tape$")))))
        #:tests? #f))
     (inputs
      (list node-irc-colors))
@@ -620,17 +591,14 @@ (define-public node-nan
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies
-                  pkg-meta
-                  '("bindings"
-                    "commander"
-                    "glob"
-                    "request"
-                    "node-gyp" ;; would be needed for tests
-                    "tap"
-                    "xtend")))))))
+             (delete-dependencies
+              '("^bindings$"
+                "^commander$"
+                "^glob$"
+                "^request$"
+                "^node-gyp$" ;; would be needed for tests
+                "^tap$"
+                "^xtend$")))))
        ;; tests need tap and other dependencies
        #:tests? #f))
     (inputs
@@ -673,21 +641,11 @@ (define-public node-addon-api
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `("benchmark"
-                                        "bindings"
-                                        "clang-format"
-                                        "eslint"
-                                        "eslint-config-semistandard"
-                                        "eslint-config-standard"
-                                        "eslint-plugin-import"
-                                        "eslint-plugin-node"
-                                        "eslint-plugin-promise"
-                                        "fs-extra"
-                                        "path"
-                                        "pre-commit"))))))
+             (delete-dependencies
+              `("^benchmark$" "^bindings$" "^clang-format$"
+                "^eslint"
+                "^fs-extra$" "^path$" "^pre-commit$"))
+             ))
          (add-after 'unpack 'skip-js-tests
            ;; We can't run the js-based tests,
            ;; but we can still do the C++ parts
@@ -767,29 +725,26 @@ (define-public node-sqlite3
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies
-                  pkg-meta
-                  `(;; Normally, this is "built" using @mapbox/node-pre-gyp,
-                    ;; which publishes or downloads pre-built binaries or
-                    ;; falls back to building from source.  Here, we patch out
-                    ;; all of that and just build directly.  It might be
-                    ;; better to patch a version of @mapbox/node-pre-gyp that
-                    ;; always builds from source, as Debian does, but there
-                    ;; are a number of dependencies that need to be packaged
-                    ;; or removed.
-                    "@mapbox/node-pre-gyp"
-                    "node-pre-gyp" ;; deprecated name still used in some places
-                    "aws-sdk"
-                    "@mapbox/cloudfriend"
-                    ;; Confusingly, this is only a dependency because of
-                    ;; @mapbox/node-pre-gyp: with that removed,
-                    ;; npm will use its own copy:
-                    "node-gyp"
-                    ;; These we'd like, we just don't have them yet:
-                    "eslint"
-                    "mocha"))))))
+             (delete-dependencies
+              `(;; Normally, this is "built" using @mapbox/node-pre-gyp,
+                ;; which publishes or downloads pre-built binaries or
+                ;; falls back to building from source.  Here, we patch out
+                ;; all of that and just build directly.  It might be
+                ;; better to patch a version of @mapbox/node-pre-gyp that
+                ;; always builds from source, as Debian does, but there
+                ;; are a number of dependencies that need to be packaged
+                ;; or removed.
+                "^@mapbox/node-pre-gyp$"
+                "^node-pre-gyp$" ;; deprecated name still used in some places
+                "^aws-sdk$"
+                "^@mapbox/cloudfriend$"
+                ;; Confusingly, this is only a dependency because of
+                ;; @mapbox/node-pre-gyp: with that removed,
+                ;; npm will use its own copy:
+                "^node-gyp$"
+                ;; These we'd like, we just don't have them yet:
+                "^eslint$"
+                "^mocha$"))))
          (add-before 'configure 'npm-config-sqlite
            ;; We need this step even if we do replace @mapbox/node-pre-gyp
            ;; because the package expects to build its bundled sqlite
@@ -885,24 +840,12 @@ (define-public node-file-uri-to-path
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `("@types/mocha"
-                                        "@types/node"
-                                        "@typescript-eslint/eslint-plugin"
-                                        "@typescript-eslint/parser"
-                                        "cpy-cli"
-                                        "eslint"
-                                        "eslint-config-airbnb"
-                                        "eslint-config-prettier"
-                                        "eslint-import-resolver-typescript"
-                                        "eslint-plugin-import"
-                                        "eslint-plugin-jsx-a11y"
-                                        "eslint-plugin-react"
-                                        "mocha"
-                                        "rimraf"
-                                        "typescript"))))))
+             (delete-dependencies
+              `("^@types/mocha$" "^@types/node$"
+                "typescript"
+                "eslint"
+                "^cpy-cli$"
+                "^mocha$" "^rimraf$"))))
          (replace 'build
            (lambda* (#:key inputs native-inputs #:allow-other-keys)
              (copy-recursively "src" "dist")
@@ -995,14 +938,12 @@ (define-public node-ms
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("eslint"
-                                                 "expect.js"
-                                                 "husky"
-                                                 "lint-staged"
-                                                 "mocha"
-                                                 "prettier")))))))
+             (delete-dependencies `("^eslint$"
+                                    "^expect\\.js$"
+                                    "^husky$"
+                                    "^lint-staged$"
+                                    "^mocha$"
+                                    "^prettier$")))))
        #:tests? #f))
     (home-page "https://github.com/vercel/ms")
     (synopsis "Tiny millisecond conversion utility")
@@ -1045,20 +986,15 @@ (define-public node-debug
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("brfs"
-                                                 "browserify"
-                                                 "coveralls"
-                                                 "istanbul"
-                                                 "karma"
-                                                 "karma-browserify"
-                                                 "karma-chrome-launcher"
-                                                 "karma-mocha"
-                                                 "mocha"
-                                                 "mocha-lcov-reporter"
-                                                 "xo"
-                                                 "supports-color")))))))
+             (delete-dependencies
+              `("^brfs$"
+                "^browserify$"
+                "^coveralls$"
+                "^istanbul$"
+                "^karma"
+                "^mocha"
+                "^xo$"
+                "^supports-color$")))))
        #:tests? #f))
     (home-page "https://github.com/debug-js/debug")
     (synopsis "Lightweight debugging utility for Node.js and the browser")
@@ -1173,12 +1109,10 @@ (define-public node-serialport-bindings
              (chdir "packages/bindings")))
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("prebuild-install"
-                                                 ;; devDependencies
-                                                 "@serialport/binding-mock"
-                                                 "node-abi"))))))
+             (delete-dependencies `("^prebuild-install$"
+                                    ;; devDependencies
+                                    "^@serialport/binding-mock$"
+                                    "^node-abi$"))))
          (add-after 'chdir 'avoid-prebuild-install
            (lambda args
              (with-atomic-json-file-replacement "package.json"
@@ -1330,11 +1264,8 @@ (define-public node-serialport-stream
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `(;; devDependencies
-                                        "@serialport/binding-mock"))))))
+             (delete-dependencies `(;; devDependencies
+                                    "^@serialport/binding-mock$"))))
          (add-after 'unpack 'chdir
            (lambda args
              (chdir "packages/stream"))))
@@ -1370,10 +1301,8 @@ (define-public node-serialport
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `("@serialport/binding-mock"))))))
+             (delete-dependencies `("^@serialport/binding-mock$"))
+             ))
          (add-after 'unpack 'chdir
            (lambda args
              (chdir "packages/serialport"))))
diff --git a/gnu/packages/node.scm b/gnu/packages/node.scm
index 5a4401e779..49e6323a9d 100644
--- a/gnu/packages/node.scm
+++ b/gnu/packages/node.scm
@@ -348,9 +348,7 @@ (define-public node-semver-bootstrap
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta '("tap")))))))))
+             (delete-dependencies '("^tap$")))))))
     (home-page "https://github.com/npm/node-semver")
     (properties '((hidden? . #t)))
     (synopsis "Parses semantic versions strings")
@@ -381,13 +379,11 @@ (define-public node-ms-bootstrap
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta '("eslint"
-                                                 "expect.js"
-                                                 "husky"
-                                                 "lint-staged"
-                                                 "mocha")))))))))
+             (delete-dependencies '("^eslint$"
+                                    "^expect\\.js$"
+                                    "^husky$"
+                                    "^lint-staged$"
+                                    "^mocha$")))))))
     (home-page "https://github.com/zeit/ms#readme")
     (properties '((hidden? . #t)))
     (synopsis "Tiny millisecond conversion utility")
@@ -417,10 +413,7 @@ (define-public node-binary-search-bootstrap
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("chai"
-                                                 "mocha")))))))))
+             (delete-dependencies `("^(chai\|mocha)$")))))))
     (home-page "https://github.com/darkskyapp/binary-search#readme")
     (properties '((hidden? . #t)))
     (synopsis "Tiny binary search function with comparators")
@@ -449,20 +442,13 @@ (define-public node-debug-bootstrap
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `("brfs"
-                                        "browserify"
-                                        "coveralls"
-                                        "istanbul"
-                                        "karma"
-                                        "karma-browserify"
-                                        "karma-chrome-launcher"
-                                        "karma-mocha"
-                                        "mocha"
-                                        "mocha-lcov-reporter"
-                                        "xo")))))))))
+             (delete-dependencies `("^brfs$"
+                                    "^browserify$"
+                                    "^coveralls$"
+                                    "^istanbul$"
+                                    "^karma"
+                                    "^mocha"
+                                    "^xo$")))))))
     (inputs (list node-ms-bootstrap))
     (home-page "https://github.com/visionmedia/debug#readme")
     (properties '((hidden? . #t)))
@@ -517,15 +503,12 @@ (define-public node-llparse-builder-bootstrap
        #:phases
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
-           (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("@types/mocha"
-                                                 "@types/node"
-                                                 "mocha"
-                                                 "ts-node"
-                                                 "tslint"
-                                                 "typescript"))))))
+           (lambda _
+             (delete-dependencies `("^@types/node$"
+                                    "mocha$"
+                                    "^ts-node$"
+                                    "^tslint$"
+                                    "^typescript$"))))
          (replace 'build
            (lambda* (#:key inputs #:allow-other-keys)
              (let ((esbuild (search-input-file inputs "/bin/esbuild")))
@@ -581,15 +564,13 @@ (define-public node-llparse-frontend-bootstrap
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta `("@types/debug"
-                                                 "@types/mocha"
-                                                 "@types/node"
-                                                 "mocha"
-                                                 "ts-node"
-                                                 "tslint"
-                                                 "typescript"))))))
+             (delete-dependencies `("^@types/debug$"
+                                    "^@types/mocha$"
+                                    "^@types/node$"
+                                    "^mocha$"
+                                    "^ts-node$"
+                                    "^tslint$"
+                                    "^typescript$"))))
          (replace 'build
            (lambda* (#:key inputs #:allow-other-keys)
              (let ((esbuild (search-input-file inputs "/bin/esbuild")))
@@ -644,18 +625,15 @@ (define-public node-llparse-bootstrap
        (modify-phases %standard-phases
          (add-after 'patch-dependencies 'delete-dependencies
            (lambda args
-             (with-atomic-json-file-replacement "package.json"
-               (lambda (pkg-meta)
-                 (delete-dependencies pkg-meta
-                                      `("@types/debug"
-                                        "@types/mocha"
-                                        "@types/node"
-                                        "esm"
-                                        "llparse-test-fixture"
-                                        "mocha"
-                                        "ts-node"
-                                        "tslint"
-                                        "typescript"))))))
+             (delete-dependencies `("^@types/debug$"
+                                    "^@types/mocha$"
+                                    "^@types/node$"
+                                    "^esm$"
+                                    "^llparse-test-fixture$"
+                                    "^mocha$"
+                                    "^ts-node$"
+                                    "^tslint$"
+                                    "^typescript$"))))
          (replace 'build
            (lambda* (#:key inputs #:allow-other-keys)
              (let ((esbuild (search-input-file inputs "/bin/esbuild")))