Error displaying messages from the console.log function within a TypeScript script

My node.js application runs smoothly with "npm run dev" and includes some typescript scripts/files. Nodemon is used to execute my code:

This is an excerpt from my package.json file:

{
  "scripts": {
    "start": "ts-node ./src/index.ts",
    "dev": "nodemon --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts"
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "@types/socket.io": "^2.1.4",
    "nodemon": "^2.0.22",
    "ts-node": "^8.10.2",
    "typescript": "^3.7.2"
  },
  "dependencies": {
    "express": "^4.17.1",
    "socket.io": "^2.3.0"
  }
}

Here is the structure of my files:

https://i.stack.imgur.com/5UA6A.png

I am trying to display console.log messages that I have written in the server.ts file, but unfortunately, they are not showing up in the console.

I attempted using the --inspect flag in the dev script:

"dev": "nodemon --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts"

by adding:

"dev": "nodemon --inspect --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts"

but encountered an error:

Unknown or unexpected option: --inspect

I also tried:

--inspect-brk

But received Unknown or unexpected option: --inspect-brk

As shown here:

https://i.stack.imgur.com/5Pgop.png

The console does not display any messages from the typescript files. I would appreciate any guidance on how to resolve this issue either with nodemon or by alternative means. Thank you in advance.

Answer №1

Using both nodemon and ts-node together doesn't allow for the --inspect flag to work smoothly.<br />
To make it work, I had to resort to using long command lines, utilizing nodemon solely for launching node</p>
<pre class="lang-json"><code>"dev": "nodemon --watch './src/**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm/transpile-only --inspect ./src/index.ts'",

If you're looking for a simpler solution, I recommend transitioning to tsx (https://github.com/esbuild-kit/tsx), where the setup is as follows:

"dev": "tsx watch --inspect ./src/index.ts"

This comes with compile caching and improved compatibility between cjs and esm modules


An alternative approach would be running it in the VSCode Javascript Debugging Terminal, which automatically configures node to run with --inspect enabled

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

Utilizing TypeScript Generics to Dynamically Set Tag Names in React

I am working on a straightforward polymorphic React component that is designed to render only tag names (such as span) and not custom React components (like MyComponent). I believe this can be achieved using JSX.IntrinsicElements. Here is the code snippet ...

Delivering a protected, locally hosted NextJS project with numerous subdomains

In the configuration of NextJS' server.js, you can ensure secure self-hosted server by including: https.createServer({ key: fs.readFileSync('./privkey.pem'), cert: fs.readFileSync('./cert.pem'), ca: fs.readFileSync('./ch ...

Tips for relocating a switcher button using Ant Design Drawer in a React application

*I'm faced with a challenge in my React project where I need to synchronize the movement of a div (switcher-btn) and an Ant Design Drawer. Whenever the Drawer is opened, I want the switcher-btn to move along with it. Below is the code snippet I'v ...

TypeScript enum type encompassing all potential values

One thing I have learned is that keyof typeof <enum> will give us a type containing all the possible keys of an enum. For example, if we have enum Season{ WINTER = 'winter', SPRING = 'spring', SUMMER = 'summer', AUT ...

Incorporate DynamoDb functionality into your Sails js application

Recently, I embarked on a project using Sailsjs with DynamoDB as the database. During my research, I came across a helpful package at https://github.com/dohzoh/sails-dynamodb. The documentation provided detailed instructions for the initial setup. After i ...

Whenever I try to execute "gulp" in the terminal, why am I encountering the message "Error: unable to locate module sass"?

Having an issue with running a gulp task on the terminal. When I try to run Terminal > Run Task... > gulp, it keeps showing me the message No gulp task found. Even when I manually type in gulp in the terminal, I still face the same error. which sass ...

The issue of receiving a "collection is not a function" TypeError often arises when trying to link MongoDB and NodeJS together

I am a newcomer to MongoDB and currently working on establishing a connection between my ExpressJS server and a MongoDB database. Below is the code I have written for this purpose: const PRIMARY_SERVER = "mongodb://localhost:27017/"; const { M ...

The process of ordering awaits using an asynchronous method

async fetchAndStoreRecords(): Promise<Records[]> { this.subRecords = await this.afs.collection<Records>('records') .valueChanges() .subscribe((data: Records[]) => { console.log('log before data ...

What is the best way to store an audio Blob in the repository file system?

Currently, I have set up a system to record user audio through the device's microphone and can successfully download it on the same device. However, my goal now is to store this audio in my database by making an API call. How can I efficiently send th ...

How do you display a GET request response onto a Jade page?

After successfully making a GET request to an API, I am now looking for a way to display the response on a Jade page. Below is the code snippet from 'index.js' located in the routes folder where the GET request is implemented. Any suggestions on ...

Error: Unable to locate module - The specified file cannot be resolved when utilizing an external JavaScript library

I am currently integrating a WYSIWYG editor (TUI Editor) into my Angular2+ application. Since there is no official Angular wrapper available, I have decided to create my own based on an existing wrapper. Due to some installation issues with npm, I saved t ...

What steps are involved in setting up a React app on a server?

I'm in the process of figuring out how to deploy my react app online. Up until now, I've been running it on my mac using npm start, and accessing it through localhost:3000 or http://127.0.0.1:3000. Recently, I purchased a small server, installe ...

Add an item with a combination of different data types (such as objects and arrays) to a Mongo database, but encountering

I am currently working on posting an item to MongoDB using a combination of Node.js, Express, Mongoose, and Vue.js. The item I am trying to post consists of a mix of objects and arrays. Although the object post is successful in generating an ID, the data i ...

React Date-Picker is unable to process a date input

Recently, I've been working on integrating a date picker into my application. I came across this helpful library that provides a date picker component: https://www.npmjs.com/package/react-date-picker So far, I have set up the component in the follow ...

The sole coding platform that fails to acknowledge the "ng" command is Visual Studio Code

Many users have encountered issues where the computer does not recognize 'ng,' but my problem differs from that. Interestingly, every software with a command shell recognizes 'ng' except for VS Code. IntelliJ? Works fine. Git bash? N ...

Issue with GitHub actions: NPM publishing encountering authentication error with code ENEEDAUTH

I have tried following the official guide for publishing and installing a package using GitHub Actions: Authenticating to package registries with granular permissions Encountered this error: npm ERR! code ENEEDAUTH npm ERR! need auth This command requires ...

The inputs for Node express middleware are unclear and lack definition

I am currently exploring Node.js as a potential replacement for my existing DOT NET API. I have created middleware to enforce basic non-role authorization in my application, but I am encountering compilation problems with the function inputs. Compilation ...

The delete function in Express is operating on the res object instead of the req object. This raises the question: if it is functioning correctly

I am currently working on creating SQL and express connections to handle insert, update, and delete operations. The insertion and updating functionalities are working perfectly fine. However, I encountered an issue when trying to perform a delete operation ...

Conquering the need for a 426 upgrade was a challenging task

For the past few months, I have been diligently working on a web application with Angular for the front end and Node/Express/Mongo for the backend. My setup involves running Angular on localhost:4200 and Node on localhost:3000. However, some members of ou ...

How can special characters be decoded in Node.js Express?

I attempted to follow the instructions provided in this resource but I am unable to decode the URI. Can someone guide me on how to proceed? For example, when I input a city like http://localhost:5000/weather?weatherCity=Malmö, the URL transforms into htt ...