All Questions
Tagged with node.js typescript
15,916
questions
739
votes
20
answers
958k
views
How to run TypeScript files from command line?
I'm having a surprisingly hard time finding an answer to this. With plain Node.JS, you can run any js file with node path/to/file.js, with CoffeeScript it's coffee hello.coffee and ES6 has babel-node ...
641
votes
24
answers
842k
views
TypeScript getting error TS2304: cannot find name ' require'
I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors.
I am getting the error "TS2304: Cannot find name 'require' " when I ...
588
votes
9
answers
362k
views
The difference between "require(x)" and "import x"
I've just started working on a small node project that will interface with a MongoDB. However, I cannot seem to get the relevant node modules to import correctly, even though I have installed them ...
480
votes
32
answers
628k
views
Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
When I try to start my app on Heroku I got the following stack trace.
It is just a basic ts.app like you see with ts-node and nodemon.
I am really interested in what the answer is going to be.
2020-05-...
342
votes
32
answers
248k
views
Extend Express Request object using Typescript
I’m trying to add a property to express request object from a middleware using typescript. However I can’t figure out how to add extra properties to the object. I’d prefer to not use bracket notation ...
302
votes
15
answers
232k
views
How to force tsc to ignore node_modules folder?
I'm using tsc build tasks. Unfortunately I'm always getting the same errors from the node modules folder
Executing task: .\node_modules\.bin\tsc.cmd --watch -p .\tsconfig.json <
node_modules/@types/...
284
votes
20
answers
415k
views
using process.env in TypeScript
How do I read node environment variables in TypeScript?
If i use process.env.NODE_ENV I have this error :
Property 'NODE_ENV' does not exist on type 'ProcessEnv'
I have installed @types/node but ...
281
votes
22
answers
346k
views
Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true
I have a database wrapper class that establishes a connection to some MongoDB instance:
async connect(connectionString: string): Promise<void> {
this.client = await MongoClient.connect(...
273
votes
14
answers
201k
views
Parsing error: Cannot read file '.../tsconfig.json'.eslint
The error Parsing error: Cannot read file '.../tsconfig.json'.eslint shows in all .ts files in the src folder including index.ts.
I have no idea how to set up configs. The issue just shows a red line ...
220
votes
3
answers
99k
views
Augmentations for the global scope can only be directly nested in external modules or ambient module declarations(2669)
I would like to store my NodeJS config in the global scope.
I tried to follow this => Extending TypeScript Global object in node.js and other solution on stackoverflow,
I made a file called global....
200
votes
12
answers
199k
views
Typescript eslint - Missing file extension "ts" import/extensions
I have a simple Node/Express app made with Typescript.
And eslint give me the error
Missing file extension "ts" for "./lib/env" import/extensions
Here is my .eslintrc file
{
...
197
votes
20
answers
60k
views
Appending .js extension on relative import statements during Typescript compilation (ES6 modules)
This seems to be a trivial problem, but it is not very obvious what settings/configurations need to be used to solve this issue.
Here are the Hello World program directory structure and the source ...
187
votes
6
answers
102k
views
Eslint says all enums in Typescript app are "already declared in the upper scope"
Starting a new application, I installed eslint and configured it with the following configs, but every time I create an enum it says it had already been defined. Even nonsense strings. Other variable ...
169
votes
7
answers
94k
views
Angular 2 Karma Test 'component-name' is not a known element
In the AppComponent, I'm using the nav component in the HTML code. The UI looks fine. No errors when doing ng serve. and no errors in console when I look at the app.
But when I ran Karma for my ...
164
votes
30
answers
205k
views
Server Discovery And Monitoring engine is deprecated
I am using Mongoose with my Node.js app and this is my configuration:
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
...
156
votes
2
answers
222k
views
TypeScript import/as vs import/require? [duplicate]
I am using TypeScript with Express/Node.js.
For consuming modules, the TypeScript Handbook shows the following syntax:
import express = require('express');
But also the typescript.d.ts file shows:
...
153
votes
9
answers
146k
views
Inject nestjs service from another module
I've got a PlayersModule and an ItemsModule.
I want to use the ItemsService in the PlayersService.
When I add it by injection:
import { Injectable } from '@nestjs/common';
import { InjectModel } ...
152
votes
9
answers
84k
views
Cannot find name 'console'. What could be the reason for this?
The following snippet shows a typescript error at LINE 4:
import {Message} from './class/message';
function sendPayload(payload : Object) : any{
let message = new Message(payload);
console.log(...
135
votes
4
answers
62k
views
What's the difference between Interceptor vs Middleware vs Filter in Nest.js?
What's the difference between an Interceptor, Filter and Middleware in Nest.js framework? When should one of them be used and favored over the other?
Thanks
134
votes
7
answers
134k
views
Typescript and Jest: Avoiding type errors on mocked functions
When wanting to mock external modules with Jest, we can use the jest.mock() method to auto-mock functions on a module.
We can then manipulate and interrogate the mocked functions on our mocked ...
134
votes
5
answers
184k
views
How to NodeJS require inside TypeScript file?
How do I load a regular NodeJS module (from node_modules) from within a TypeScript class?
When I try to compile .ts file that contains:
var sampleModule = require('modulename');
Compiler prompts that ...
132
votes
5
answers
46k
views
Having error "Module 'name' resolves to an untyped module at..." when writing custom TypeScript definition file
I can't find TypeScript definition @type/{name} for one of my installed NodeJS packages, so I attempt to write a d.ts file for it, and put the file in {project root}\typings folder. This is how I do:
...
129
votes
23
answers
123k
views
Mongoose the Typescript way...?
Trying to implement a Mongoose model in Typescript. Scouring the Google has revealed only a hybrid approach (combining JS and TS). How would one go about implementing the User class, on my rather ...
127
votes
15
answers
127k
views
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag
I'm trying to make a simple API using typescript and NodeJS but when I run my code I get this error
"This module is declared with using 'export =', and can only be used
with a default import ...
127
votes
5
answers
117k
views
How to consume npm modules from typescript?
I'm giving a shot at typescript. It works fine at the hello world stage. I'm now trying to use a npm module :
index.ts =
import _ = require('lodash')
console.log(_.toUpper('Hello, world !'))
This ...
124
votes
1
answer
76k
views
Upgrading Jest to v29 - Error Test environment jest-environment-jsdom cannot be found
Has anyone successfully upgraded to latest Jest version 29?
I'm receiving an error:
Error: Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option ...
123
votes
12
answers
139k
views
Cannot find module 'ts-node/register'
I want to use mocha to test my TypeScript/Angular2 project. I tried to use ts-node as described here:
npm install -g ts-node
but when running
mocha --require ts-node/register -t 10000 ./**/*....
121
votes
20
answers
279k
views
tsc is not recognized as internal or external command
I updated from VSCode 0.10.6 to 0.10.8, and tried using Typescript for the first time. Unfortunately I when I tell VSCode to build, I get the error:
tsc is not a recognized as an internal or ...
117
votes
8
answers
61k
views
where to find or how to set htmlWebpackPlugin.options.title in project created with vue cli 3?
I wanted to set title to my webpage created with vue cli 3 and thus looked into public/index.html. There, I found <title><%= htmlWebpackPlugin.options.title %></title>.
How do I set ...
116
votes
17
answers
174k
views
Typescript Error: Property 'user' does not exist on type 'Request'
I have the following piece of code in my express app
router.get('/auth/userInfo', this.validateUser, (req, res) => {
res.json(req.user);
});
and my IDE seems to be complaining with the error
...
113
votes
8
answers
363k
views
Read and write a text file in typescript
How should I read and write a text file from typescript in node.js?
I am not sure would read/write a file be sandboxed in node.js, if not, i believe there should be a way in accessing file system.
111
votes
6
answers
39k
views
Writing npm modules in TypeScript
I am working on my first npm module. I briefly worked with TypeScript before and a big problem was that for many modules there were no definition files available. So I thought it would be a good idea ...
108
votes
8
answers
68k
views
TypeScript isNaN only accepts a number
I work with WebStorm 2016.2.2, TypeScript 2.1, Node.js.
For some reason, isNaN is declared as a function that only accepts a number:
declare function isNaN(number: number): boolean;
I tried to change ...
106
votes
10
answers
108k
views
Extending TypeScript Global object in node.js
I have a node.js app that attaches some config information to the global object:
global.myConfig = {
a: 1,
b: 2
}
The TypeScript compiler doesn't like this because the Global type has no ...
106
votes
8
answers
192k
views
How to install and run Typescript locally in npm?
I want to install and run Typescript (i.e. no global dependencies).
Here is my package.json file:
{
"name": "foo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
...
105
votes
5
answers
266k
views
How to use query parameters in Nest.js?
I am a freshman in Nest.js.
And my code as below
@Get('findByFilter/:params')
async findByFilter(@Query() query): Promise<Article[]> {
}
I have used postman to test this router
http://...
104
votes
5
answers
94k
views
typescript error - cannot find name 'process'
I am setting up a new project with express+ typescript and facing typescript error - cann't find name 'processs'
package.json
"dependencies": {
"express": "^4.16.4",
"nodemon": "^1.18.7",
...
100
votes
9
answers
148k
views
guid/uuid in Typescript Node.js app
I try to make a uuid (v 3.0.1) package work in Node/Typescript app, but I'm not sure what should I import and how to use it.
This is index.d.ts (from @types/uuid v 2.0.29):
declare namespace uuid {
...
100
votes
1
answer
47k
views
Relationship between the version of node.js and the version of @types/node
I'm just started using node and wanted to try it with typescript. I did an npm install @types/node and the latest version was 7.0.4:
$ npm install @types/node
[email protected] /home/...
96
votes
3
answers
50k
views
TypeScript tsconfig settings for Node.js 12?
What is the optimal TypeScript tsconfig settings for outputting code that's going to be run on Node.js 12?
92
votes
28
answers
199k
views
Angular: Cannot Get /
I am trying to open, build and run someone else's Angular 4 project but I am not able to view the project when I run it my way. I don't see what is going wrong or what I should do now. I already had ...
88
votes
3
answers
42k
views
What's the difference between tsc (TypeScript compiler) and ts-node?
I'm very confused about the difference between tsc and ts-node. I'm learning TypeScript and I usually transpile server .ts files with tsc command.
Now, I'm approaching nestjs framework, and I see ...
85
votes
5
answers
123k
views
What is the nestjs error handling approach (business logic error vs. http error)?
While using NestJS to create API's I was wondering which is the best way to handle errors/exception.
I have found two different approaches :
Have individual services and validation pipes throw new ...
84
votes
8
answers
59k
views
package.json `exports` field not working with TypeScript
The version 13.2 of Node.js allows ESM modules and a new package.json field, called exports, to select and rewrite exported files.
Before 13.2, I was importing files from the dist folder of my ...
82
votes
13
answers
107k
views
jest ReferenceError: Cannot access '' before initialization
I'm getting the error:
ReferenceError: Cannot access 'myMock' before initialization
Even though i respected jest documentation about the hoisting:
A limitation with the factory parameter is that, ...
82
votes
4
answers
207k
views
Use fs in typescript
I'm just trying to read a file using fs.readFileSync, though it seems it cannot be found.
I made sure to declare it, added it within my constructor:
export default class Login extends React....
81
votes
5
answers
68k
views
How to dynamically access object property in TypeScript
I've been going through and trying to convert existing projects (from Node.js) to TypeScript.
For context, I'm using the http-status package (https://www.npmjs.com/package/http-status)
I'm trying to ...
81
votes
7
answers
376k
views
No overload matches this call. Type 'string' is not assignable to type 'Signals'
I am using typescript to build a microservice and handling signals as well. The code was working fine till a few days ago but recently it started throwing errors. Couldn't find a fix for the issue.
...
80
votes
7
answers
30k
views
UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON with Jest + Angular
Using the jest-preset-angular to perform the unit test, but got an warning as UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON not sure what causing the error, due to ...
79
votes
3
answers
95k
views
Remove everything after last backslash
var t = "\some\route\here"
I need "\some\route" from it.
Thank you.