Posts

Showing posts with the label nodejs

tilde (~) and a caret (^) in a npm package.json file

Image
What is tilde (~) and a caret (^) in an npm package.json file? If you are using npm to manage your app, then you know that we will use package.json for managing dependencies. sample package.json file So as we can see in the above sample package.json file we have some dependencies & dev dependencies as well, now let's go close to dependencies and try to understand the meaning of tilde & caret symbols. { "devDependencies": { "@angular/common": "5.0.0", "cordova-plugin-splashscreen": "^5.0.3", }, "devDependencies": { "@ionic/app-scripts": "3.2.4", "@types/chart.js": "^2.7.37", "typescript": "~2.6.2" } } As we can see above JSON from the package.json file,  key is the name of the package and the value is the version of the package to be used. The version number is in semver syntax which designates each s...

MongoDB remove unique constraint

Image
MongoDB Remove the unique constraint: Src: https://en.wikipedia.org/wiki/MongoDB Today, I'm working on a RESTful API Using Node.js With Express and Mongoose example, and I ran into a problem with the MongoDB Schema: POST : { username: 'vikaskad' , email: 'testmail@test.com' , name : 'Vikas Kad' } { [ MongoError : E11000 duplicate key error index : ecomm_database . users . $email_1 dup key : { : "testmail@test.com" }] name : 'MongoError' , err : 'E11000 duplicate key error index: ecomm_database.users.$email_1 dup key: { : "testmail@test.com " }' , and my schema is as follows: var Product = new Schema ({ username : { type : String , required : true }, email : { type : String ,unique:true, required : true }, name : { type : String }, createdAt : { type : Date , default : Date . now } }); but after some database inser...

PM2 cheat sheet | PM2 commands | PM2 tutorial

Image
PM2 PM2 (Process Manager): PM2 is a process manager for Node.JS apps when I want to keep node app to run in the background mostly use pm2 for it. I wanted to understand all its commands and its use so I went through its document, and write down some useful commands, then I thought let's share this as cheatsheet with all of you might be this can be useful for all of you. PM2 Cheatsheet: Start-Process: pm2 start server.js --name crudapp It will start server.js app with name as crud app. pm2 start server.js --no-daemon It will run pm2 daemon in the foreground if it doesn’t exist already. pm2 start server.js --no-autorestart It will not auto restart if the server stops. pm2 start server.js --no-vizion It will skip the versioning control mode. pm2 start server.js -i 0 It will start maximum processes with LB depending on available CPUs. pm2 start all It will start all existing process and restart if already ru...

Nodejs auto restart server | NodeJS watch file Changes

Image
NodeJS Logo(Source: Wikipedia) When we working with nodejs application manually restarting our application is the tedious task for everyone, so everytime when we working on node application then we may think that How to watch node file changes or just simply  watch node file changes right? So we will see how can we overcome this problem with simple step by using nodemon to watch file changes. Step 1: Create sample application with the following command: $ npm init -y #-y for accpeting default configuration Wrote to /home/ubuntu/dev/blogTutor/package.json: { "name": "blogTutor", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "Vikas", "license": "ISC" } Step 2: ...

Visual Studio: Cannot find runtime 'node' on PATH

Image
Visual Studio code error: Cannot find runtime 'node' on path Error. If you are working on NodeJS   Project and using Visual Studio Code as your IDE. Visual Studio Code provides debugging NodeJS application feature as well. To Run Debugger you just need to Select Debugger from Menu and start debugging.    But if you are using nvm on your machine then it may happen that you are using the different version to run node application and Visual Studio Code uses different node version. Sometimes Visual Studio Code doesn't find node so it will throw the following error. And if you see the launch.json file it will look like below. { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version" : "0.2.0" , "configurations" : [ { "type" : "node" , "reque...