Repository: nuxt/nuxt.js
Pull request #8194 (2020-10-13 10:11:59)

fix(cli): avoid error about `nuxt-edge` if installed in parent `node_modules`

<!--- Provide a general summary of your changes in the title above -->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [x] Bug fix (a non-breaking change which fixes an issue)
- [ ] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)


## Description

Following up https://github.com/nuxt/nuxt.js/discussions/7973, originally we wanted to prevent installing both `nuxt` and `nuxt-edge` on same project but with current implementation we also check all parent `node_modules`. This is problematic if for example `/docs` need different distro of nuxt. this PR also switches to a soft warning.

To keep alias support (#6051), we compare real package.json names (d407dc7f3315d1acaff77f74560d421e8fa8dc11)

## Checklist:
<!--- Put an `x` in all the boxes that apply. -->
<!--- If your change requires a documentation PR, please link it appropriately -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly. (PR: #)
- [ ] I have added tests to cover my changes (if not applicable, please state why)
- [ ] All new and existing tests are passing.



codecov-io: # [Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=h1) Report
> Merging [#8194](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=desc) into [dev](https://codecov.io/gh/nuxt/nuxt.js/commit/202f35584be8f3f875dc73720ec71f9e643873a6?el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/nuxt/nuxt.js/pull/8194/graphs/tree.svg?width=650&height=150&src=pr&token=nGD1PtMB3M)](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##              dev    #8194      +/-   ##
==========================================
- Coverage   68.92%   68.89%   -0.04%     
==========================================
  Files          91       91              
  Lines        3855     3851       -4     
  Branches     1049     1049              
==========================================
- Hits         2657     2653       -4     
  Misses        973      973              
  Partials      225      225              
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `68.89% <100.00%> (-0.04%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/cli/src/run.js](https://codecov.io/gh/nuxt/nuxt.js/pull/8194/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2xpL3NyYy9ydW4uanM=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=footer). Last update [202f355...c44cf74](https://codecov.io/gh/nuxt/nuxt.js/pull/8194?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/cli/src/run.js b/packages/cli/src/run.js
index b9fde5758..468758992 100644
--- a/packages/cli/src/run.js
+++ b/packages/cli/src/run.js
@@ -1,24 +1,19 @@
+import { existsSync } from 'fs'
+import { resolve } from 'path'
 import execa from 'execa'
+import consola from 'consola'
 import { name as pkgName } from '../package.json'
 import NuxtCommand from './command'
 import setup from './setup'
 import getCommand from './commands'
 import { isNuxtDir } from './utils/dir'
 
-function packageExists (name) {
-  try {
-    require.resolve(name)
-    return true
-  } catch (e) {
-    return false
-  }
-}
-
 export default async function run (_argv, hooks = {}) {
   // Check for not installing both nuxt and nuxt-edge
-  const dupPkg = '@nuxt/' + (pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge')
-  if (packageExists(dupPkg)) {
-    throw new Error('Both `nuxt` and `nuxt-edge` dependencies are installed! This is unsupported, please choose one and remove the other one from dependencies.')
+  const dupPkg = pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge'
+  const dupPkgJSON = resolve(__dirname, '../..' /* dist/../.. */, dupPkg, 'package.json')
+  if (existsSync(dupPkgJSON) && require(dupPkgJSON).name !== '@nuxt/' + dupPkg) {
+    consola.warn('Both `nuxt` and `nuxt-edge` dependencies are installed! Please choose one and remove the other one from dependencies.')
   }
 
   // Read from process.argv


Test patch:
diff --git a/packages/cli/test/unit/run-edge.test.js b/packages/cli/test/unit/run-edge.test.js
index 366f91c74..3a7057844 100644
--- a/packages/cli/test/unit/run-edge.test.js
+++ b/packages/cli/test/unit/run-edge.test.js
@@ -1,12 +1,18 @@
+import consola from 'consola'
+import getCommand from '../../src/commands'
 import run from '../../src/run'
 
+jest.mock('../../src/commands')
+
 jest.mock('../../package.json', () => ({
   name: '@nuxt/cli-edge'
 }))
 
 describe('run in edge', () => {
   test('throws error if nuxt and nuxt-edge are installed', async () => {
-    await expect(run())
-      .rejects.toThrow('Both `nuxt` and `nuxt-edge` dependencies are installed! This is unsupported, please choose one and remove the other one from dependencies.')
+    const mockedCommand = { run: jest.fn(() => Promise.resolve({})) }
+    getCommand.mockImplementationOnce(() => Promise.resolve(mockedCommand))
+    await run()
+    expect(consola.warn).toHaveBeenCalledWith('Both `nuxt` and `nuxt-edge` dependencies are installed! Please choose one and remove the other one from dependencies.')
   })
 })