Ad

Npm Install With --production Option And Peer Dependencies

I'm using npm v2 and for some specific reason I need to bundle react-dom with browserify.

However I faced a problem with installing its' dependencies. My steps:

  • download react-dom .tar file from npm registry (http://registry.npmjs.org/) and unpack it
  • go to the react-dom folder and run npm install --production but that results in error:

    react-dom dmitri$ npm install --production
    npm ERR! Darwin 15.3.0
    npm ERR! argv "node" "/usr/local/bin/npm" "install" "--production"
    npm ERR! node v0.12.7
    npm ERR! npm  v2.11.3
    
    npm ERR! Cannot read property 'react' of undefined
    npm ERR!
    npm ERR! If you need help, you may report this error at:
    npm ERR!     <https://github.com/npm/npm/issues>
    
    npm ERR! Please include the following file with any support request:
    npm ERR!     /Users/dmitri/github/browserify-cdn-test/node_modules/react-dom/npm-debug.log
    

Interesting but if run npm install without --production flag it gets peer dependencies without errors.

Any workaround?

UPDATE posting npm-debug.log file contents:

    cat /Users/dmitri/github/react-dom-tar/npm-debug.log
    0 info it worked if it ends with ok
    1 verbose cli [ 'node', '/usr/local/bin/npm', 'install', '--production' ]
    2 info using [email protected]
    3 info using [email protected]
    4 verbose readDependencies loading dependencies from /Users/dmitri/github/react-dom-tar/package.json
    5 verbose install where, deps [ '/Users/dmitri/github/react-dom-tar', [] ]
    6 verbose stack TypeError: Cannot read property 'react' of undefined
    6 verbose stack     at /usr/local/lib/node_modules/npm/lib/install.js:179:33
    6 verbose stack     at Array.forEach (native)
    6 verbose stack     at /usr/local/lib/node_modules/npm/lib/install.js:178:50
    6 verbose stack     at /usr/local/lib/node_modules/npm/lib/install.js:367:22
    6 verbose stack     at evalmachine.<anonymous>:263:20
    6 verbose stack     at OpenReq.Req.done (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:141:5)
    6 verbose stack     at OpenReq.done (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:61:22)
    6 verbose stack     at FSReqWrap.oncomplete (evalmachine.<npm/node_modules/graceful-fs/graceful-fs.js:61:22)
    6 verbose stack     at FSReqWrap.oncomplete (evalmachine.<anonymous>:95:15)
    7 verbose cwd /Users/dmitri/github/react-dom-tar
    8 error Darwin 15.3.0
    9 error argv "node" "/usr/local/bin/npm" "install" "--production"
    10 error node v0.12.7
    11 error npm  v2.11.3
    12 error Cannot read property 'react' of undefined
    13 error If you need help, you may report this error at:
    13 error     <https://github.com/npm/npm/issues>
    14 verbose exit [ 1, true ]

UPDATE package.json file contents:

{
  "name": "react-dom",
  "version": "0.14.6",
  "description": "React package for working with the DOM.",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/facebook/react.git"
  },
  "keywords": [
    "react"
  ],
  "license": "BSD-3-Clause",
  "bugs": {
    "url": "https://github.com/facebook/react/issues"
  },
  "homepage": "https://github.com/facebook/react/tree/master/npm-react-dom",
  "peerDependencies": {
    "react": "^0.14.6"
  },
  "scripts": {
    "start": "node server.js"
  },
  "_id": "[email protected]",
  "_shasum": "46d4e3243884f277e89ce8759131e8a16ac23e65",
  "_resolved": "https://registry.npmjs.org/react-dom/-/react-dom-0.14.6.tgz",
  "_from": "[email protected]*",
  "_npmVersion": "2.14.15",
  "_nodeVersion": "4.2.2",
  "_npmUser": {
    "name": "zpao",
    "email": "[email protected]"
  },
  "maintainers": [
    {
      "name": "spicyj",
      "email": "[email protected]"
    },
    {
      "name": "zpao",
      "email": "[email protected]"
    }
  ],
  "dist": {
    "shasum": "46d4e3243884f277e89ce8759131e8a16ac23e65",
    "tarball": "http://registry.npmjs.org/react-dom/-/react-dom-0.14.6.tgz"
  },
  "directories": {},
  "readme": "ERROR: No README data found!"
}
Ad

Answer

So after investigation it's clear that this is a NPM bug - https://github.com/npm/npm/issues/6581

It appears when no dependencies property is present in package.json, peerDependencies is present and npm install is ran with --production flag. Simplest way to repro:

 echo '{"peerDependencies": { "npm": "*" } }' > package.json
 npm install --production

Before it will be fixed in order to be on the safe side, package maintainers need to add empty object todependencies: {}.

Ad
source: stackoverflow.com
Ad