npm ls requires peer dependencies to be installed

What I Wanted to Do

Package an rpm with fpm which calls npm ls to validate the dependencies.

What Happened Instead

npm ls fails with the following error message:

npm ERR! peer dep missing: typescript@*, required by ts-pnp@1.0.1

The project builds and runs just fine. We don’t intend to use typescript for it.

Reproduction Steps

  1. Create a blank react app npx create-react-app demo
  2. Install production dependencies npm install --production
  3. Run npm ls

Details

I believe the problem is npm ls requires peer dependencies be installed, I don’t think this should be the behaviour.

Platform Info

$ npm --versions
{ demo: '0.1.0',
  npm: '6.5.0',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '34.0',
  http_parser: '2.8.0',
  icu: '63.1',
  llhttp: '1.0.1',
  modules: '67',
  napi: '4',
  nghttp2: '1.34.0',
  node: '11.9.0',
  openssl: '1.1.1a',
  tz: '2018e',
  unicode: '11.0',
  uv: '1.25.0',
  v8: '7.0.276.38-node.16',
  zlib: '1.2.11' }
$ node -p process.platform
darwin

That seems appropriate behaviour for npm, given you are calling npm ls to see if the dependencies are correct, and they are not?

$ npx create-react-app demo
$ cd demo
$ npm install --production
$ npm ls
...
npm ERR! peer dep missing: typescript@*, required by ts-pnp@1.0.1
$ more node_modules/ts-pnp/package.json | grep 'peerDependencies"' -A 2
  "peerDependencies": {
    "typescript": "*"
  },

Hi,

I was under the impression that as this is a peer dependency it is not required, and so this would be unusual behavior from npm ls?

Cheers.

Peer dependencies are required by the package listing them.

https://docs.npmjs.com/files/package.json#peerdependencies

NOTE: npm versions 1 and 2 will automatically install peerDependencies if they are not explicitly depended upon higher in the dependency tree. In the next major version of npm (npm@3), this will no longer be the case. You will receive a warning that the peerDependency is not installed instead.

You may have been thinking of optional dependencies: https://docs.npmjs.com/files/package.json#optionaldependencies

It looks like this may be an issue in the ts-pnp package then. I’ll do some digging and see if something needs raising on their end.

Cheers :slight_smile:

PS: This can be closed now.

2 Likes