From 69e0c4e8cd684c475a4450c40dfb32c995061aea Mon Sep 17 00:00:00 2001 From: nlf Date: Wed, 17 Feb 2021 13:38:06 -0800 Subject: [PATCH] throw an error when trying to dedupe in global mode PR-URL: https://github.com/npm/cli/pull/2716 Credit: @nlf Close: #2716 Reviewed-by: @ljharb, @wraithgar --- lib/dedupe.js | 6 ++++++ test/lib/dedupe.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/dedupe.js b/lib/dedupe.js index fe8243e21e43..5e455192bcab 100644 --- a/lib/dedupe.js +++ b/lib/dedupe.js @@ -10,6 +10,12 @@ const completion = require('./utils/completion/none.js') const cmd = (args, cb) => dedupe(args).then(() => cb()).catch(cb) const dedupe = async (args) => { + if (npm.flatOptions.global) { + const er = new Error('`npm dedupe` does not work in global mode.') + er.code = 'EDEDUPEGLOBAL' + throw er + } + const dryRun = (args && args.dryRun) || npm.flatOptions.dryRun const where = npm.prefix const arb = new Arborist({ diff --git a/test/lib/dedupe.js b/test/lib/dedupe.js index ff2d2be53405..b14185525bbe 100644 --- a/test/lib/dedupe.js +++ b/test/lib/dedupe.js @@ -1,6 +1,21 @@ const { test } = require('tap') const requireInject = require('require-inject') +test('should throw in global mode', (t) => { + const dedupe = requireInject('../../lib/dedupe.js', { + '../../lib/npm.js': { + flatOptions: { + global: true, + }, + }, + }) + + dedupe([], er => { + t.match(er, { code: 'EDEDUPEGLOBAL' }, 'throws EDEDUPEGLOBAL') + t.end() + }) +}) + test('should remove dupes using Arborist', (t) => { const dedupe = requireInject('../../lib/dedupe.js', { '../../lib/npm.js': {