35,466
questions
3232
votes
35
answers
2.3m
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 ...
875
votes
24
answers
332k
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 ...
1225
votes
49
answers
2.1m
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:...
241
votes
7
answers
38k
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 ...
768
votes
38
answers
908k
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 ...
194
votes
4
answers
152k
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, ...
173
votes
15
answers
211k
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 ...
207
votes
6
answers
147k
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 ...
1770
votes
34
answers
2.9m
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 ...
199
votes
5
answers
71k
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 ...
5429
votes
22
answers
1.9m
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 ...
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": [
...
767
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) {
...
1562
votes
13
answers
529k
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 ...
2396
votes
68
answers
3.1m
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 (...
1511
votes
40
answers
708k
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 ...
203
votes
27
answers
147k
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: ...
1837
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, ...
1601
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 ...
1517
votes
22
answers
979k
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",
"...
2113
votes
18
answers
2.1m
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?
891
votes
32
answers
945k
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
...
371
votes
15
answers
826k
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 ...
290
votes
9
answers
669k
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(...
532
votes
18
answers
865k
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: ...
506
votes
12
answers
809k
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 (...
492
votes
10
answers
1.1m
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 ...
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
...
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 ...
2937
votes
17
answers
1.0m
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 ...
920
votes
24
answers
959k
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" ...
573
votes
11
answers
716k
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 ...
1262
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
4k
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 ...
205
votes
17
answers
103k
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.
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 ...
1145
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?
853
votes
12
answers
878k
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 ...
39
votes
4
answers
18k
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 ...
359
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 ...
1138
votes
12
answers
443k
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 ...
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:...
454
votes
19
answers
1.2m
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 ...
721
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 ...
682
votes
33
answers
1.8m
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 ...
9
votes
1
answer
19k
views
Discord.js v13 code breaks when upgrading to v14
I've just updated my discord.js from v13 to v14 and there are many errors.
Errors with message and interaction events:
Neither message nor interaction events fire.
Errors with intents:
const client = ...
1467
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 ...
10
votes
3
answers
6k
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 ...
669
votes
34
answers
898k
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.
22
votes
1
answer
37k
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: '...