Heroku build is reporting that it cannot locate the `@types` in the package.json file

Encountered Heroku Build Error

- TSError: ⨯ Struggling to compile TypeScript:
- src/server.ts(1,38): error TS7016: File declaration for module 'express' not found. '/app/node_modules/express/index.js' is implicitly of type 'any'.
- Try `npm i --save-dev @types/express` if available or add a new declaration (.d.ts) file containing `declare module 'express';`
- src/server.ts(10,14): error TS2580: Name 'process' cannot be found. Need to install type definitions for node? Attempt `npm i --save-dev @types/node`.

Stuck for quite some time while attempting to deploy my code on Heroku, the most recent error is displayed above and I'm uncertain about what modifications are required to make it functional. I've been adhering to instructions and have included @types/express, @types/node, etc. in my package.json script. Nevertheless, they aren't being recognized. Feeling unsure about the next steps, any assistance would be welcomed.

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "baseUrl": "./src",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "lib": ["es2015"],
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "src/**/*.test.ts"]
}

Procfile

web:ts-node/src/server.ts 

src/server.ts

import express, { Application } from 'express';
import { routes } from './routes';

// Initialize express
export const app: Application = express();

// Defining application routes
routes(app);

const port = process.env.PORT || 5000;

// Starting server
app.listen(port, () => console.log(`Server is listening on port ${port}!`));

package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "main": "src/server.ts",
  "type": "commonjs",
  "scripts": {
    "start": "ts-node src/server.ts",
    "dev": "nodemon -x ts-node src/server.ts",
    "build": "tsc",
    "test": "jest --coverage"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  },
  "devDependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/node": "^16.7.6",
    "@types/jest": "^26.0.24",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^4.29.0",
    "@typescript-eslint/parser": "^4.29.0",
    "eslint": "^7.32.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "husky": "^7.0.1",
    "jest": "^27.0.6",
    "nodemon": "^2.0.12",
    "prettier": "^2.3.2",
    "pretty-quick": "^3.1.1",
    "supertest": "^6.1.4",
    "ts-jest": "^27.0.4"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "ts-node": "^10.1.0",
    "typescript": "^4.3.5"
  },
  "description": ""
}

Answer №1

Solved the puzzle.

During the build process, Heroku removes dev dependencies which caused a problem because all my @types were in the devDependencies section and hence were inaccessible, leading to the error mentioned earlier.

package.json

  // Added engine specifications as well.
  // Following the recommendation in the documentation, I made sure to align my configurations with the development environment for better compatibility.
  "engines": {
    "node": "15.x",
    "npm": "7.x"
  },
...
  "dependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/node": "^16.7.6",
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "ts-node": "^10.2.1",
    "typescript": "^4.4.2"
  }

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

Encountering an error on Linux while installing node: The version `GLIBC_2.27' required by node is not found at /lib64/libm.so.6

I recently tried to follow the steps outlined in this post about installing a node.tar.xz file in Linux: How to install node.tar.xz file in linux However, when I try running `node --version`, I encounter some errors. You can view the errors here. I&apos ...

What is the process for inputting a predefined function into an interface?

In my project, I have a Locale interface that defines the properties of a locale for my component: interface Locale { src: string; alt: string; language: string; i18nFormat: string; } During debugging, I am using the built-in .toSource() function ...

Whenever I try to log in using axios and my credentials are incorrect, I don't receive any response back in the catch block

I cannot receive any response when calling the login API from Node.js. In the frontend, I am handling the catch as well. How do I retrieve the Invalid Credentials message from the backend API if the credentials do not match? The code for my backend logi ...

The regex routes are now unable to efficiently serve static assets

Question Is it possible to use regex or the built-in URL processor in Express to properly load static files? Expected Behavior Express should match the initial route it encounters and load files as usual. Actual Behavior Error messages indicate that ...

Is there a way to access the active request being processed in a node.js environment?

I am currently working with express.js and I have a requirement to log certain request data whenever someone attempts to log a message. To accomplish this, I aim to create a helper function as follows: function logMessage(level, message){ winston.log(le ...

When working with mongoose, there seems to be a delay in queries when utilizing mongoose.createConnection()

Working Version: var mongoose = require('mongoose'); var db = function() { return { config: function(conf) { mongoose.connect('mongodb://' + conf.host + '/' + conf.database); var db = mongoose.connection; ...

Capturing information transmitted from an input device

I'm currently working on capturing input data from a piano connected to my computer via USB. $ lsusb ... Bus 003 Device 046: ID fc08:0101 .... The piano is identified as Bus 003 Device 046: ID fc08:0101. When I try $ cat /dev/bus/usb/003/046, th ...

Unable to retrieve data from MongoDB with the given query

I have encountered a problem while following the documentation mentioned in this link. To build my website, I am using mongodb, express, and node. In my application, I have a user collection that stores users' data. I intend to display a JSON repres ...

Issue with holding session in Mongoose-Auth/Express/Connect not being resolved

Currently, I am in the process of developing a node js app and attempting to implement a user authentication system using Mongoose-Auth. Despite following several tutorials and guides on how to use Mongoose-Auth, I have encountered a hurdle. The basic pas ...

`Resolving issue with "exports" not being defined when running my Node.js project

Out of nowhere, I encountered an issue while trying to run npm start on my project. To my surprise, it displayed the following errors: https://i.stack.imgur.com/mWRI9.png ...

Updating a property value within a JSON object: Adjusting attributes in a JSON data format

How can I modify a specific key's value in a JSON array like the following example: input = [{"201708":10,"201709": 12, "metric":"attritionManaged"},{"201708":10,"201709": 12, "metric":"attritionUnManaged"},{"201708":10,"201709": 12, "metric":"EHC"}] ...

Navigating multiple databases while managing a single model in Mongoose

I am looking to create a system with 50 arbitrary databases, each containing the same collections but different data. I want to use a single Node.js web application built with ExpressJS and Mongoose. Here is a simplified example: In this scenario: The ...

Module for managing optional arguments in Node.js applications

I'm on the hunt for a Node.js module that can effectively manage and assign optional arguments. Let's consider a function signature like this: function foo(desc, opts, cb, extra, writable) { "desc" and "cb" are mandatory, while everything else ...

Understanding JSON Parsing in Jade

I am facing a challenge with handling a large array of objects that I am passing through express into a Jade template. The structure of the data looks similar to this: [{ big object }, { big object }, { big object }, ...] To pass it into the Jade templat ...

Can you please explain the counterpart of Date() in NodeJS for Python functions?

Can Python provide a function similar to Date() in NodeJS? Here is an example of the output from Node's Date() function: https://i.stack.imgur.com/oYvs2.png I am looking for the equivalent format in Python. ...

Execute a selector on child elements using cheerio

I am struggling to apply selectors to elements in cheerio (version 1.0.0-rc.3). Attempting to use find() results in an error. const xmlText = ` <table> <tr><td>Foo</td><td/></tr> <tr><td>1,2,3</td> ...

Using TypeScript to chain observables in a service and then subscribing to them in the component at the end

Working with Platform - Angualar 2 + TypeScript + angularFire2 Within my user.service.ts file, I have implemented the following code to initiate an initial request to a firebase endpoint in order to fetch some path information. Subsequently, I aim to util ...

Error: postman expected an array instead of a string

I encountered an issue while trying to submit form data with multiple fields sharing the same name. The error message "TypeError: expected string but received array" keeps popping up. My suspicion is that the problem lies within Postman. I intend to have ...

What is the best way to programmatically route or navigate to a specific route within a Next.js class component?

tag: In the process of creating my app with next.js, I primarily use functional components. The sole exception is a class component that I utilize to manage forms. Upon form submission, my goal is to redirect back to the home page. However, when I attemp ...

Implementing virtual hosts and HTTPS encryption

Is it possible to configure vhosts on Express with https? This is my current code without SSL: var express = require('express'); var vhost = require('vhost'); var path = require('path'); var appOne = express(); var appTwo = ...