While trying React Tutorial, I ran into several issues of same nature which were fairly easy to fix. Recording the process of their resolution here in case some one can benefit from it.
First of all, I cloned the project using following command and then tried running provided node server:
$ git clone git@github.com:reactjs/react-tutorial.git
$ cd react-tutorial; node server.js
But it returned with an error:
node.js:201
throw e; // process.nextTick error, or ‘error’ event on first tick
^
Error: Cannot find module ‘body-parser’
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
.
.
.
at Array.0 (module.js:479:10)
That means that I don’t have body-parser installed which should be specified in package.json, so I guess I have to install it by executing following command in the project’s main directory:
$ npm install
Nopes. This returned with another error saying:
Error: failed to fetch from registry: body-parser
Okay. So my node’s registry do not have body-parser. Following should fix the registry issue:
$ npm config set registry http://registry.npmjs.org/; npm install
This time I did get 200 http://registry.npmjs.org/body-parser, which means the last command worked but down the line hit a new error:
npm ERR! Error: No compatible version found: body-parser@’^1.4.3′
npm ERR! Valid install targets:
npm ERR! [“1.0.0″,”1.0.1″,”1.0.2”]
Version incompatibility. Sweet.
So now, I can either use an older version of body-parser or instead update the other pieces of puzzle. Let’s go with the later solution. Following may not update node and npm but still let’s try:
$ sudo apt-get install node
$ sudo apt-get install npm
Tried running the server again but this time following error popped up:
$ node server.js
node.js:201
throw e; // process.nextTick error, or ‘error’ event on first tick
^
Error: Cannot find module ‘express’
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
.
.
.
at Array.0 (module.js:479:10)
That means, I don’t have express. How can that happen? Express should have gotten installed along with body-parser when `npm install` was executed above as express is specified in package.json just like body-parser. Anyway, let’s explicitly install express:
$ npm install express
Oh. Following error is reported by command above. Okay, this explains why express didn’t get installed i.e. the version incompatibility played its negative / positive role there.
npm http GET https://registry.npmjs.org/express
npm ERR! Error: No compatible version found: express@’^4.4.5′
npm ERR! Valid install targets:
.
.
.
npm not ok
At this point, I realized that my node version was still old i.e. 0.6.12 which meant that last time I tried updating node by simply installing it again, its latest version did not get installed. Therefore, in order to upgrade to its latest version I followed: this stackoverflow question but it also didn’t work out as evident from error given below:
$ sudo npm cache clean -f
$ sudo npm install -g n
npm http GET https://registry.npmjs.org/nnpm ERR! Error: failed to fetch from registry: n
npm ERR! at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12
npm ERR! at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9)
npm ERR! at Request._callback (/usr/share/npm/lib/utils/npm-registry-client/request.js:136:18)
.
.
.
npm ERR! Additional logging details can be found in:
npm ERR! /home/usman/work/react-tutorial/npm-debug.log
npm not ok
Finally, tried purging node and npm, re-installed them as well as required modules by the project and then retried running the server. This time it worked fine. Now, I am all set to continue with the tutorial.
$ sudo apt-get purge nodejs npm
$ curl -sL https://deb.nodesource.com/setup | sudo bash –
$ sudo apt-get install -y nodejs
$ node -v
v0.10.40
$ npm -v
1.4.28$ npm install express
$ node server.js
module.js:340
throw err;
^
Error: Cannot find module ‘body-parser’
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/usman/work/react-tutorial/server.js:16:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)$ npm install
body-parser@1.14.1 node_modules/body-parser
├── bytes@2.1.0
├── content-type@1.0.1
├── depd@1.1.0
├── qs@5.1.0
├── on-finished@2.3.0 (ee-first@1.1.1)
├── http-errors@1.3.1 (inherits@2.0.1, statuses@1.2.1)
├── raw-body@2.1.4 (unpipe@1.0.0)
├── debug@2.2.0 (ms@0.7.1)
├── iconv-lite@0.4.12
└── type-is@1.6.9 (media-typer@0.3.0, mime-types@2.1.7)$ node server.js
Server started: http://localhost:3000/