Skip to main content
Filter by
Sorted by
Tagged with
3291 votes
35 answers
2.4m views

Using async/await with a forEach loop

Are there any issues with using async/await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function ...
Saad's user avatar
  • 53.9k
878 votes
24 answers
338k views

How do I convert an existing callback API to promises?

I want to work with promises but I have a callback API in a format like: ###1. DOM load or other one time event: window.onload; // set to callback ... window.onload = function() { }; ###2. Plain ...
Benjamin Gruenbaum's user avatar
1235 votes
49 answers
2.2m views

Error: Can't set headers after they are sent to the client

I'm fairly new to Node.js and I am having some issues. I am using Node.js 4.10 and Express 2.4.3. When I try to access http://127.0.0.1:8888/auth/facebook, i'll be redirected to http://127.0.0.1:...
DjangoRocks's user avatar
  • 14.2k
241 votes
7 answers
40k views

When is .then(success, fail) considered an antipattern for promises?

I had a look at the bluebird promise FAQ, in which it mentions that .then(success, fail) is an antipattern. I don't quite understand its explanation as for the try and catch. What's wrong with the ...
user2127480's user avatar
  • 4,741
771 votes
39 answers
924k views

Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?

I am trying to support CORS in my Node.js application that uses the Express.js web framework. I have read a Google group discussion about how to handle this, and read a few articles about how CORS ...
mikong's user avatar
  • 8,370
200 votes
4 answers
158k views

Is it an anti-pattern to use async/await inside of a new Promise() constructor?

I'm using the async.eachLimit function to control the maximum number of operations at a time. const { eachLimit } = require("async"); function myFunction() { return new Promise(async (resolve, ...
user avatar
176 votes
15 answers
215k views

setState doesn't update the state immediately [duplicate]

I would like to ask why my state is not changing when I do an onClick event. I've search a while ago that I need to bind the onClick function in constructor but still the state is not updating. Here's ...
Sydney Loteria's user avatar
212 votes
6 answers
154k views

async function implicitly returns promise?

I read that async functions marked by the async keyword implicitly return a promise: async function getVal(){ return await doSomethingAync(); } var ret = getVal(); console.log(ret); but that is ...
Alexander Mills's user avatar
1788 votes
34 answers
3.0m views

How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)

My version of node is always v0.6.1-pre even after I install brew node and NVM install v0.6.19. My node version is: node -v v0.6.1-pre NVM says this (after I install a version of node for the first ...
Dominic Tancredi's user avatar
201 votes
5 answers
72k views

Is it bad practice to have a constructor function return a Promise?

I'm trying to create a constructor for a blogging platform and it has many async operations going on inside. These range from grabbing the posts from directories, parsing them, sending them through ...
adam-beck's user avatar
  • 6,009
5521 votes
23 answers
2.0m views

What's the difference between tilde(~) and caret(^) in package.json?

After I upgraded to the latest stable node and npm, I tried npm install moment --save. It saves the entry in the package.json with the caret ^ prefix. Previously, it was a tilde ~ prefix. Why are ...
Fizer Khan's user avatar
  • 92.9k
60 votes
2 answers
43k views

Updating a Nested Array with MongoDB

I am trying to update a value in the nested array but can't get it to work. My object is like this { "_id": { "$oid": "1" }, "array1": [ ...
masanorinyo's user avatar
  • 1,128
770 votes
30 answers
1.3m views

How to process POST data in Node.js?

How do you extract form data (form[method="post"]) and file uploads sent from the HTTP POST method in Node.js? I've read the documentation, googled and found nothing. function (request, response) { ...
Ming-Tang's user avatar
  • 17.7k
1561 votes
13 answers
530k views

What is the purpose of Node.js module.exports and how do you use it?

What is the purpose of Node.js module.exports and how do you use it? I can't seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source ...
mrwooster's user avatar
  • 24.2k
1516 votes
40 answers
723k views

How to fix npm throwing error without sudo

I just installed node and npm through the package on nodejs.org, and whenever I try to search or install something with npm, it throws the following error unless I sudo the command. I have a feeling ...
Chad's user avatar
  • 18.4k
2521 votes
71 answers
3.4m views

Error message "error:0308010C:digital envelope routines::unsupported"

I created the default IntelliJ IDEA React project and got this: Error: error:0308010C:digital envelope routines::unsupported at new Hash (node:internal/crypto/hash:67:19) at Object.createHash (...
a1cd's user avatar
  • 25.3k
203 votes
27 answers
149k views

Get Download URL from file uploaded with Cloud Functions for Firebase

After uploading a file in Firebase Storage with Functions for Firebase, I'd like to get the download url of the file. I have this : ... return bucket .upload(fromFilePath, {destination: ...
Valentin's user avatar
  • 5,608
1858 votes
41 answers
2.9m views

map function for objects (instead of arrays)

I have an object: myObject = { 'a': 1, 'b': 2, 'c': 3 } I am looking for a native method, similar to Array.prototype.map that would be used as follows: newObject = myObject.map(function (value, ...
Randomblue's user avatar
  • 116k
1611 votes
27 answers
2.0m views

How to get GET (query string) variables in Express.js on Node.js?

Can we get the variables in the query string in Node.js just like we get them in $_GET in PHP? I know that in Node.js we can get the URL in the request. Is there a method to get the query string ...
XMen's user avatar
  • 30.3k
1539 votes
22 answers
1.0m views

How can I get the full object in Node.js's console.log(), rather than '[Object]'?

I have this object: const myObject = { "a":"a", "b":{ "c":"c", "d":{ "e":"e", "...
user avatar
2124 votes
19 answers
2.2m views

Writing to files in Node.js

I've been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?
Gjorgji's user avatar
  • 23.1k
898 votes
32 answers
956k views

How can I print a circular structure in a JSON-like format?

I have a big object I want to convert to JSON and send. However it has circular structure, so if I try to use JSON.stringify() I'll get: TypeError: Converting circular structure to JSON or ...
Harry's user avatar
  • 55k
290 votes
9 answers
684k views

Why is my asynchronous function returning Promise { <pending> } instead of a value?

My code: let AuthUser = data => { return google.login(data.username, data.password).then(token => { return token } ) } And when i try to run something like this: let userToken = AuthUser(...
Src's user avatar
  • 5,502
513 votes
12 answers
836k views

CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true

I have a setup involving Frontend server (Node.js, domain: localhost:3000) <---> Backend (Django, Ajax, domain: localhost:8000) Browser <-- webapp <-- Node.js (Serve the app) Browser (...
ixaxaar's user avatar
  • 6,769
375 votes
15 answers
850k views

await is only valid in async function

I wrote this code in lib/helper.js: var myfunction = async function(x,y) { .... return [variableA, variableB] } exports.myfunction = myfunction; Then I tried to use it in another file: var ...
j.doe's user avatar
  • 4,879
532 votes
18 answers
871k views

Node.js - SyntaxError: Unexpected token import

I don't understand what is wrong. Node v5.6.0 NPM v3.10.6 The code: function (exports, require, module, __filename, __dirname) { import express from 'express' }; The error: SyntaxError: ...
SofDroid's user avatar
  • 5,509
504 votes
12 answers
1.2m views

How to completely remove node.js from Windows

I uninstalled my previous version of node.js (0.8.11) and downloaded the latest, 0.10.24 from the node.js website and installed it. However, after running node --version, it still indicates that I'm ...
Andrew De Forest's user avatar
5 votes
1 answer
8k views

change directory command cd ..not working in batch file after npm install

I have the following command in one bat file. echo STEP12 cd HDC git config --global url."https://".insteadOf git:// echo STEP13 cd hui-components-style npm install --registry http://localhost:23510 ...
Deepak Kumar Padhy's user avatar
502 votes
52 answers
1.5m views

How to solve npm error "npm ERR! code ELIFECYCLE"

I'm trying to learn react, so I have this sample code for the full-stack react voting app, and I am trying to get it to work but after running npm install followed by npm start I receive the following ...
user avatar
2976 votes
18 answers
1.1m views

What's the difference between dependencies, devDependencies, and peerDependencies in NPM package.json file?

This documentation answers my question very poorly. I didn't understand those explanations. Can someone say in simpler words? Maybe with examples if it's hard to choose simple words? Also added ...
Vitalii Korsakov's user avatar
922 votes
24 answers
968k views

How to access POST form fields in Express

Here is my simple form: <form id="loginformA" action="userlogin" method="post"> <div> <label for="email">Email: </label> <input type="text" id="email" ...
murvinlai's user avatar
  • 50.4k
584 votes
11 answers
735k views

How can I use an ES6 import in Node.js? [duplicate]

I'm trying to get the hang of ES6 imports in Node.js and am trying to use the syntax provided in this example: Cheatsheet Link I'm looking through the support table, but I was not able to find what ...
Jonathan002's user avatar
  • 10.4k
1261 votes
3 answers
1.3m views

How do I get started with Node.js [closed]

Are there any good resources to get started with Node.JS? Any good tutorials, blogs or books? Of course, I have visited its official website http://nodejs.org/, but I didn't think the documentation ...
4 votes
4 answers
5k views

Can't connect to MongoDB 6.0 Server locally using Node.js driver

I have just started learning about MongoDB and I am trying to host my Node.js application locally via MongoDB Server 6.0 (without using Mongoose or Atlas). I copied the async JavaScript code given in ...
Sushant's user avatar
  • 69
209 votes
17 answers
106k views

Google Firestore - How to get several documents by multiple ids in one round-trip?

I am wondering if it's possible to get multiple documents by a list of ids in one round trip (network call) to the Firestore database.
Joon's user avatar
  • 9,914
26 votes
2 answers
12k views

Removing nested promises

I'm new to promises and writing network code using requests and promises in NodeJS. I would like to remove these nested promises and chain them instead, but I'm not sure how I'd go about it/whether ...
Tarrence's user avatar
  • 794
1150 votes
24 answers
1.6m views

How is an HTTP POST request made in node.js?

How can I make an outbound HTTP POST request, with data, in node.js?
Mark's user avatar
  • 70k
868 votes
12 answers
908k views

Execute a command line binary with Node.js

I am in the process of porting a CLI library from Ruby over to Node.js. In my code I execute several third party binaries when necessary. I am not sure how best to accomplish this in Node. Here's an ...
Dave Thompson's user avatar
34 votes
6 answers
35k views

How can I scrape pages with dynamic content using node.js?

I am trying to scrape a website but I don't get some of the elements, because these elements are dynamically created. I use the cheerio in node.js and My code is below. var request = require('...
JayD's user avatar
  • 19.4k
39 votes
4 answers
19k views

Chained promises not passing on rejection

I am have a problem understanding why rejections are not passed on through a promise chain and I am hoping someone will be able to help me understand why. To me, attaching functionality to a chain of ...
Jordan's user avatar
  • 721
10 votes
3 answers
7k views

Correct way to write a non-blocking function in Node.js

I wrote a simple function that returns Promise so should be non-blocking (in my opinion). Unfortunately, the program looks like it stops waiting for the Promise to finish. I am not sure what can be ...
nosbor's user avatar
  • 3,009
1138 votes
12 answers
446k views

Node.js + Nginx - What now?

I've set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions: How should they work together? How should I handle the requests? There are 2 concepts for ...
Van Coding's user avatar
  • 24.6k
101 votes
6 answers
95k views

Querying after populate in Mongoose

I'm pretty new to Mongoose and MongoDB in general so I'm having a difficult time figuring out if something like this is possible: Item = new Schema({ id: Schema.ObjectId, dateCreated: { type:...
jschr's user avatar
  • 1,886
460 votes
19 answers
1.3m views

How do I debug error ECONNRESET in Node.js?

I'm running an Express.js application using Socket.io for a chat webapp and I get the following error randomly around 5 times during 24h. The node process is wrapped in forever and it restarts itself ...
723 votes
8 answers
1.1m views

What is "require" in JavaScript and NodeJS?

I'm trying to get JavaScript to read/write to a PostgreSQL database. I found this project on GitHub. I was able to get the following sample code to run in Node. var pg = require('pg'); //native libpq ...
neuromancer's user avatar
  • 55.6k
363 votes
40 answers
1.7m views

"SyntaxError: Unexpected token < in JSON at position 0"

In a React app component which handles Facebook-like content feeds, I am running into an error: Feed.js:94 undefined "parsererror" "SyntaxError: Unexpected token < in JSON at position 0 I ran ...
Cameron Sima's user avatar
  • 5,385
705 votes
32 answers
1.9m views

SyntaxError: Cannot use import statement outside a module

I've got an ApolloServer project that's giving me trouble, so I thought I might update it and ran into issues when using the latest Babel. My "index.js" is: require('dotenv').config() import ...
user3810626's user avatar
  • 7,709
1497 votes
25 answers
1.1m views

Sending command line arguments to npm script

The scripts portion of my package.json currently looks like this: "scripts": { "start": "node ./script.js server" } ...which means I can run npm start to start the ...
user avatar
678 votes
34 answers
914k views

How to download a file with Node.js (without using third-party libraries)?

How do I download a file with Node.js without using third-party libraries? I don't need anything special. I only want to download a file from a given URL, and then save it to a given directory.
greepow's user avatar
  • 7,323
22 votes
1 answer
38k views

message.content doesn't have any value in Discord.js

With discord v14, I was trying to use the messageCreate event, however, after a user types a message in discord, message.content doesn't have any data as shown below: Message { channelId: '...
user19530601's user avatar

1
2 3 4 5
715