NodeJS API development tools for production deployment

I am new to NodeJS and currently working on a NodeJS API project that is hosted on Render with node_env in production.

However, when running the project in a cloud environment, I encountered the following error:

"Error: Cannot find module 'morgan'"

This seems to be due to a missing dev dependency library. Initially, I set up the hosting platform's build command to install libraries using npm install, but then switched to npm install --dev which resolved the issue.

My questions are:

  1. As my code contains both prod and dev environments, I have used if statements to disable dev code during production. However, I still need to install dev dependencies when hosting the server. What is the best practice for managing dev and prod environments in a NodeJS API?
  2. Is there a way to avoid installing dev dependencies in the production environment?

Package.json

  "name": "bnndirect_api",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "express-mongo-sanitize": "^2.2.0",
    "express-rate-limit": "^7.1.5",
    "helmet": "^7.1.0",
    "hpp": "^0.2.3",
    "perfect-express-sanitizer": "^1.0.13"
  },
  "devDependencies": {
    "morgan": "^1.10.0",
    "nodemon": "^3.0.2"
  }
}

Snippet of dev code in server.js utilizing the 'morgan' library

    const morgan = require("morgan");
// DEV LOGGING MIDDLEWARE
    if (process.env.NODE_ENV === 
    "development") {
    app.use(morgan("dev"));
    }

Answer №1

Prior to installation, you're already incorporating morgan. For instance, in your production setting, morgan hasn't been installed yet. However, you have included a require('morgan') statement there that will attempt to utilize morgan in your code. Consider updating your code as follows:

// LOGGING MIDDLEWARE FOR DEVELOPMENT
if (process.env.NODE_ENV === "development") {
    const morgan = require("morgan");
    app.use(morgan("dev"));
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The updates made to the Heroku Node.js file are not being retained

I have a basic Discord bot with a leveling system running on node.js, where all user stats are being stored in a .json file. Everything seems to be working correctly, but when I attempt to view the file through Heroku's bash cat command, it only show ...

Encountering numerous errors while attempting to upgrade from Angular 9 to Angular 11

On Thursday of last week, I initiated an Angular update and unfortunately, I have been encountering numerous errors ever since. The most recent error while running ng serve -o is: An unhandled exception occurred: ENOENT: no such file or directory, lstat &a ...

Combining the power of ExpressJS with a dynamic blend of ejs and React for an

My current setup involves a NodeJS application with an Express backend and EJS for the frontend. The code snippet below shows an example route: router.get("/:name&:term", function(req, res) { Course.find({ courseName: req.params.name, courseTerm: req.p ...

I'm in the process of putting together a node.js project using typescript, but I'm a little unsure about the steps needed to

Currently, I am working on a node.js project that involves compiling with typescript. I recently realized that there is a directory named scripts dedicated to running various tasks outside of the server context, such as seed file operations. With files now ...

cPanel is incompatible with node version 12.16.0

I am facing a dilemma regarding hosting my node API, which was built using node version 12.16.0, on cPanel. The available version for node in cPanel is 12.9.0 (Most recent). How should I proceed? Is the node version really a critical factor in this case? ...

Even in the face of errors, Selenium with Node.js continues to run seamlessly. The challenge arises specifically with the 107.xx version of the Chrome browser and Chrome driver

Currently, I am employed in a project involving NODE JS (javascript) with selenium webdriver. Package.json- “chai”: “^4.3.6”, “chromedriver”: “^107.0.3”, “geckodriver”: “^3.2.0”, “mocha”: “^10.0.0”, “mochawesome”: “^7. ...

What is the best way to invoke the first exported function from the second exported function?

I am looking to create a file containing four or five exported functions. exports.firstFunction = function() { // some code }; exports.secondFunction = function() { // need to call firstFunction }; My issue is that I want the second expo ...

Ways to retrieve response data from the server using angularjs

In my current setup, I have a Node.js server handling the authentication process while AngularJS is used on the frontend. When a user clicks a button to sign in with Facebook, the server takes care of all authentication aspects and redirects to the URI of ...

Is there a way to navigate to a new page by utilizing the onClick properties of Material UI table actions in a React application?

When working with a Material UI table, I am utilizing an Action to view details of my data. Now, I am looking to navigate to a new page or render a new component when this action button is clicked. Can someone assist me in achieving this ...

Converting a stringified array object to an object using Expressjs

When working with Angular, I am sending stringified data using FormData. Here is an example: this.formData.append('data', JSON.stringify(this.collections)) Now my challenge is converting this string back to an object in my Express backend. The d ...

Navigating with Angular 2: Expressing HTML 5 Routing Challenges

I'm currently working on a simple web application using Express 4 and Angular 2. The only Angular 2 specific aspect in this project is the utilization of its HTML5 router. Let me walk you through how the routing works in this app: There are two prim ...

What is the best way to refresh a Windows 7 command prompt screen before executing a new function in Node.js?

I attempted system calls for cls and also tested out this code snippet: function clear() { process.stdout.write('\u001B[2J\u001B[0;0f'); } Unfortunately, none of the options seem to be effective. ...

transferring data using local host in Node.js

The code snippet below shows how app1.js is sending information to localhost port 3000: //app1.js var http = require('http'); const valueToTransfert = 'test'; var server = http.createServer(function(req, res) { ...

Issue with MEAN stack: NPM is unable to locate the module 'webpack/lib/optimize/CommonsChunkPlugin' causing an error

I seem to be encountering an issue while trying to upgrade to the latest version of webpack. Upon investigating various threads, it appears that the error is caused by a deprecated class. Webpack migration 3 -> 4: Error: Cannot find module 'webpack ...

install jquery package via npm and save it as a dependency

Currently using Ubuntu 16.04 Proxy settings are configured in ~/.npmrc, here is the setup: registry="http://registry.npmjs.org/" proxy="http://username:password@proxyconfig:port" strict-ssl=false http-proxy="http://username:password@proxyconfig:port" ht ...

I'm curious to learn more about the architecture of GO lang. How does it compare in terms of speed to Node.js? And what specific factors contribute to its speed

Could you provide a breakdown of the architecture behind GO language? How does its speed compare to Nodejs, and what are the factors that contribute to this faster performance? It is known that Go is developed using C/C++, so can it outperform C/C++ in t ...

Is it advantageous to deploy a node.js production with NGINX and PM2 simultaneously?

As a beginner in Node.js, I recently completed building my first Node.js server. To enhance the performance of my server in production, I have been exploring various options, such as NGINX and Process Manager (PM2). NGINX: It offers load balancing for i ...

Establish global connections for the waterline model without using sails.js

In my current project, I have successfully implemented Waterline even though it is not a sails project. Running on version 0.11.x, I found that setting the connection in each model ensures everything works smoothly (as shown in the commented out line below ...

Comparing Node.js streaming with the sendFile method

During my download speed tests, I compared using res.sendFile(src); and fs.createReadStream(src).pipe(res);, but didn't notice a significant difference. Which method is more efficient for serving files, and why? Will streaming be better suited for han ...

What is the process for converting an <input type="file> into a base64 string?

Currently, I'm attempting to send an image to my express backend by including the image directly in the post request body. var imgValue = document.getElementById("image").value; Within my post request: body : JSON.stringify({ image:imgValue ...