Sunday, March 15, 2020

Error: Cannot find module 'express'



Error Blog 

Hi Guys ,
This blog is about the error   Error: Cannot find module 'express'

Problem Statement

unable to run a file using node js .


Error Details 

error Log

ERROR DETAILS 
 Error: Cannot find module 'express'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> 
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)



Error Code 

Please validate the API endpoint .

  
     var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('/'function (reqres) {
   res.send('Hello World');
})

var server = app.listen(8082function () {
   var host = server.address().address;
   var port = server.address().port;

   console.log("Example app listening at http://%s:%s"hostport)
})  
         

Solution

use npm install to install the required dependencies


Code Changes 

Install express

  
         npm install express
         
OUTPUT
node server.js     
Example app listening at http://:::8082




Thanks for reading . Let me know your thoughts in the comments below    .

Saturday, February 29, 2020

Uncaught (in promise) SyntaxError: Unexpected end of JSON input



Error Blog 

Hi Guys ,
This blog is about the error   Uncaught (in promise) SyntaxError: Unexpected end of JSON input

Problem Statement

Unable to see the response from  a http call in react js .


Error Details 

error Log

ERROR DETAILS 
 Uncaught (in promise) SyntaxError: Unexpected end of JSON input
    at <anonymous>:31:28




Solution

The http call does not return a json output . .


Code Changes 

Please validate the API endpoint .

  componentDidMount() {
        fetch(`/api/users`)
            .then(res => res.json())
            .then(result => this.setState({ users: result.users }))
    }
OUTPUT





Thanks for reading . Let me know your thoughts in the comments below    .

Inline Babel script: 'this' is not allowed before super()



Error Blog 

Hi Guys ,
This blog is about the error   Inline Babel script: 'this' is not allowed before super()
 

Problem Statement

Unable to load a html page in react js .


Error Details 

error Log

ERROR DETAILS 
 babel.min.js:4 Uncaught SyntaxError: Inline Babel script: 'this' is not allowed before super()
  3 |      constructor() {
  4 |         
> 5 | this.state = {
    | ^
  6 |             users: []
  7 |         }
  8 |     }
    at r.buildCodeFrameError (babel.min.js:4)
    at e.buildCodeFrameError (babel.min.js:23)
    at e.ThisExpression (babel.min.js:7)
    at e.o [as _call] (babel.min.js:22)
    at e.a [as call] (babel.min.js:22)
    at e.l [as visit] (babel.min.js:22)
    at e.visitQueue (babel.min.js:22)
    at e.visitSingle (babel.min.js:22)
    at e.visit (babel.min.js:22)
    at Function.v.node (babel.min.js:22)




Solution

As the error suggested , we modified the code and added super before using this .


Code Changes 

List of files changed

 constructor() {
        super();
        this.state = {
            users: []
        }
    }
OUTPUT





Thanks for reading . Let me know your thoughts in the comments below    .

Node js Web Server

Start a http server using node js .

Install http-server
npm install http-server -g
Start it 
cd MyApp
http-server
Starting up http-server, serving ./
Available on:
  http://192.138.1.4:8081
  http://169.214.159.112:8081
  http://127.0.0.1:8081
Hit CTRL-C to stop the server
Open the browser and type 
http://192.138.1.4:8081
You can see the index.html file being served .
In the console you will see the following output :
[Sun Mar 01 2020 13:08:41 GMT+0530 (India Standard Time)]  "GET /" "Mozilla/5.0 (Linux; Android 8.0.0; moto x4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.119 Mobile Safari/537.36"
[Sun Mar 01 2020 13:08:42 GMT+0530 (India Standard Time)]  "GET /favicon.ico" "Mozilla/5.0 (Linux; Android 8.0.0; moto x4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.119 Mobile Safari/537.36"
[Sun Mar 01 2020 13:08:42 GMT+0530 (India Standard Time)]  "GET /favicon.ico" Error (404): "Not found"
[Sun Mar 01 2020 13:08:55 GMT+0530 (India Standard Time)]  "GET /FirstApp.html" "Mozilla/5.0 (Linux; Android 8.0.0; moto x4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.119 Mobile Safari/537.36"



Thursday, February 27, 2020

Sunday, February 23, 2020

Argument passed to when() is not a mock!



Error Blog 

Hi Guys ,
This blog is about the error   Argument passed to when() is not a mock! .
 

Problem Statement

This error will come when we try mock a class using Mockito and junit .


Error Details 

error Log

ERROR DETAILS 


org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();

at service.DbServiceTest.saveTest(DbServiceTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Solution

What made this fix 


Code Changes 

List of files changed

OUTPUT
Result 




Thanks for reading . Let me know your thoughts in the comments below    .
   

ec2-user@ec2 Permission denied