diff --git a/packages/core/src/package.ts b/packages/core/src/package.ts index d50afe980..0eb942cfe 100644 --- a/packages/core/src/package.ts +++ b/packages/core/src/package.ts @@ -168,15 +168,30 @@ export default class Package { @Memoize() get dependencies(): Package[] { let names = flatMap(this.dependencyKeys, key => Object.keys(this.packageJSON[key] || {})); - return names.map(name => { - if (this.nonResolvableDeps) { - let dep = this.nonResolvableDeps.get(name); - if (dep) { - return dep; + return names + .map(name => { + if (this.nonResolvableDeps) { + let dep = this.nonResolvableDeps.get(name); + if (dep) { + return dep; + } } - } - return this.packageCache.resolve(name, this); - }); + try { + return this.packageCache.resolve(name, this); + } catch (error) { + // if the package was not found do not error out here. this is relevant + // for the case where a package might be an optional peerDependency and we dont + // want to error if it was not found. Additionally, erroring here is "far" away + // from the actual logical failure point and so not failing here will provide a better + // error message down the line + if (error.code === 'MODULE_NOT_FOUND') { + return false; + } + + throw error; + } + }) + .filter(Boolean) as Package[]; } hasDependency(name: string): boolean { diff --git a/packages/core/tests/package.test.ts b/packages/core/tests/package.test.ts index 3e436290d..a3a0c04e5 100644 --- a/packages/core/tests/package.test.ts +++ b/packages/core/tests/package.test.ts @@ -71,4 +71,43 @@ describe('package', () => { expect(nonResolvableDeps.get('good')).toBeTruthy(); } }); + + test('it does not throw if the peerDependency is not resolvable', () => { + let { name: tmpLocation } = tmp.dirSync(); + let projectJSON = { + 'package.json': JSON.stringify({ + name: 'foobar-web', + dependencies: { + good: '*', + }, + peerDependencies: { + bad: '*', + 'valid-peer': '*', + }, + }), + node_modules: { + good: { + 'package.json': JSON.stringify({ + name: 'good', + }), + }, + 'valid-peer': { + 'package.json': JSON.stringify({ + name: 'valid-peer', + }), + }, + }, + }; + + fixturify.writeSync(tmpLocation, projectJSON); + + let packageCache = new PackageCache(); + let packageInstance = new Package(tmpLocation, packageCache); + let dependencies = packageInstance.dependencies; + + expect(dependencies.length).toBe(2); + expect(dependencies.map(dep => dep.name)).toContain('good'); + expect(dependencies.map(dep => dep.name)).toContain('valid-peer'); + expect(dependencies.map(dep => dep.name)).not.toContain('bad'); + }); });