All Questions
Tagged with node.js javascript
177,362
questions
3162
votes
34
answers
2.2m
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 ...
3063
votes
41
answers
1.8m
views
How do I pass command line arguments to a Node.js program and receive them?
I have a web server written in Node.js and I would like to launch with a specific folder. I'm not sure how to access arguments in JavaScript. I'm running node like this:
$ node server.js folder
here ...
2191
votes
17
answers
585k
views
How to decide when to use Node.js?
I am new to this kind of stuff, but lately I've been hearing a lot about how good Node.js is. Considering how much I love working with jQuery and JavaScript in general, I can't help but wonder how to ...
2105
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?
1815
votes
41
answers
2.8m
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, ...
1754
votes
36
answers
2.8m
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 ...
1708
votes
42
answers
551k
views
How do I debug Node.js applications?
How do I debug a Node.js server application?
Right now I'm mostly using alert debugging with print statements like this:
sys.puts(sys.inspect(someVariable));
There must be a better way to debug. I ...
1608
votes
33
answers
1.6m
views
How do you get a list of the names of all files present in a directory in Node.js?
I'm trying to get a list of the names of all the files present in a directory using Node.js. I want output that is an array of filenames. How can I do this?
1561
votes
13
answers
527k
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 ...
1554
votes
11
answers
913k
views
Read environment variables in Node.js
Is there a way to read environment variables in Node.js code?
Like for example Python's os.environ['HOME'].
1507
votes
23
answers
1.2m
views
Where does npm install packages?
Can someone tell me where can I find the Node.js modules, which I installed using npm?
1495
votes
20
answers
943k
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",
"...
1440
votes
25
answers
1.0m
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 ...
1318
votes
11
answers
836k
views
Using Node.js require vs. ES6 import/export
In a project I am collaborating on, we have two choices on which module system we can use:
Importing modules using require, and exporting using module.exports and exports.foo.
Importing modules using ...
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 ...
1227
votes
29
answers
1.1m
views
In Node.js, how do I "include" functions from my other files?
Let's say I have a file called app.js. Pretty simple:
var express = require('express');
var app = express.createServer();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app....
1214
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:...
1035
votes
31
answers
1.1m
views
How to parse JSON using Node.js? [closed]
How should I parse JSON using Node.js? Is there some module which will validate and parse JSON securely?
1020
votes
19
answers
743k
views
How to list npm user-installed packages
How do I list the user-installed / environment package only in npm?
When I do npm -g list, it outputs every package and their dependencies. Instead I'd like to see the packages installed in the ...
925
votes
32
answers
763k
views
How can I run multiple npm scripts in parallel?
In my package.json I have these two scripts:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
}
I have to run these 2 scripts in parallel ...
919
votes
24
answers
948k
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" ...
891
votes
12
answers
768k
views
What is "export default" in JavaScript?
File: SafeString.js
// Build out our basic SafeString type
function SafeString(string) {
this.string = string;
}
SafeString.prototype.toString = function() {
return "" + this.string;
};
...
888
votes
32
answers
935k
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
...
884
votes
12
answers
557k
views
Call async/await functions in parallel
As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallel. ...
871
votes
24
answers
327k
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 ...
864
votes
24
answers
350k
views
module.exports vs exports in Node.js
I've found the following contract in a Node.js module:
module.exports = exports = nano = function database_module(cfg) {...}
I wonder what's the difference between module.exports and exports and why ...
852
votes
14
answers
774k
views
Using Node.JS, how do I read a JSON file into (server) memory?
Background
I am doing some experimentation with Node.js and would like to read a JSON object, either from a text file or a .js file (which is better??) into memory so that I can access that object ...
839
votes
12
answers
849k
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 ...
834
votes
54
answers
1.1m
views
Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object
I am getting this error:
Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
...
828
votes
48
answers
770k
views
Babel 6 regeneratorRuntime is not defined
I'm trying to use async/await from scratch on Babel 6, but I'm getting regeneratorRuntime is not defined.
.babelrc file
{
"presets": [ "es2015", "stage-0" ]
}
...
809
votes
31
answers
755k
views
Error: request entity too large
I'm receiving the following error with express:
Error: request entity too large
at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/...
769
votes
27
answers
1.4m
views
How can I wait In Node.js (JavaScript)? l need to pause for a period of time
I'm developing a console script for personal needs. I need to be able to pause for an extended amount of time, but, from my research, Node.js has no way to stop as required. It’s getting hard to read ...
769
votes
31
answers
731k
views
Read a file one line at a time in node.js?
I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together.
var Lazy=require("...
767
votes
18
answers
831k
views
How to append to a file in Node?
I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.
fs.writeFile('log.txt', 'Hello Node', function (err) {
if (err) throw err;...
718
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 ...
708
votes
17
answers
269k
views
Node.js on multi-core machines
Node.js looks interesting, BUT I must miss something - isn't Node.js tuned only to run on a single process and thread?
Then how does it scale for multi-core CPUs and multi-CPU servers? After all, it ...
673
votes
11
answers
552k
views
Installing a local module using npm?
I have a downloaded module repo, I want to install it locally, not globally in another directory?
What is an easy way to do this?
669
votes
33
answers
882k
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.
668
votes
33
answers
1.7m
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 ...
654
votes
12
answers
371k
views
How to print a stack trace in Node.js?
Does anyone know how to print a stack trace in Node.js?
642
votes
33
answers
457k
views
Invalidating JSON Web Tokens
For a new node.js project I'm working on, I'm thinking about switching over from a cookie based session approach (by this, I mean, storing an id to a key-value store containing user sessions in a user'...
631
votes
16
answers
532k
views
Fastest way to copy a file in Node.js
The project that I am working on (Node.js) implies lots of operations with the file system (copying, reading, writing, etc.).
Which methods are the fastest?
618
votes
21
answers
424k
views
Can I use jQuery with Node.js?
Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?
609
votes
12
answers
322k
views
How can I write a test which expects an 'Error' to be thrown in Jasmine?
I'm trying to write a test for the Jasmine Test Framework which expects an error. At the moment I'm using a Jasmine Node.js integration from GitHub.
In my Node.js module I have the following code:
...
591
votes
15
answers
221k
views
Is it not possible to stringify an Error using JSON.stringify?
Reproducing the problem
I'm running into an issue when trying to pass error messages around using web sockets. I can replicate the issue I am facing using JSON.stringify to cater to a wider audience:
...
574
votes
31
answers
505k
views
How to auto-reload files in Node.js?
Any ideas on how I could implement an auto-reload of files in Node.js? I'm tired of restarting the server every time I change a file.
Apparently Node.js' require() function does not reload files if ...
568
votes
26
answers
946k
views
ReferenceError: fetch is not defined
I have this error when I compile my code in node.js, how can I fix it?
RefernceError: fetch is not defined
This is the function I am doing, it is responsible for recovering information from a ...
563
votes
36
answers
1.0m
views
Node.js heap out of memory
Today I ran my script for filesystem indexing to refresh RAID files index and after 4h it crashed with following error:
[md5:] 241613/241627 97.5%
[md5:] 241614/241627 97.5%
[md5:] 241625/...
562
votes
23
answers
570k
views
React - changing an uncontrolled input
I have a simple react component with the form which I believe to have one controlled input:
import React from 'react';
export default class MyForm extends React.Component {
constructor(props) {
...
546
votes
45
answers
891k
views
How to fix Error: listen EADDRINUSE while using NodeJS?
If I run a server with the port 80, and I try to use XMLHttpRequest I am getting this error: Error: listen EADDRINUSE
Why is it problem for NodeJS, if I want to do a request, while I run a server on ...