Questions tagged [node.js]
Node.js is an event-based, non-blocking, asynchronous I/O runtime that uses Google's V8 JavaScript engine and libuv library. It is used for developing applications that make heavy use of the ability to run JavaScript both on the client as well as on the server side and therefore benefit from the re-usability of code and the lack of context switching.
472,498
questions
661
votes
31
answers
851k
views
How do I run a node.js app as a background service?
Since this post has gotten a lot of attention over the years, I've listed the top solutions per platform at the bottom of this post.
Original post:
I want my node.js server to run in the background, ...
659
votes
6
answers
301k
views
npm install vs. update - what's the difference?
What is the practical difference between npm install and npm update? When should I use which?
658
votes
13
answers
392k
views
How to find unused packages in package.json?
Is there a way to determine if you have packages in your package.json file that are no longer needed?
For instance, when trying out a package and later commenting or deleting code, but forgetting to ...
655
votes
9
answers
154k
views
What is Node.js' Connect, Express and "middleware"?
Despite knowing JavaScript quite well, I'm confused what exactly these three projects in Node.js ecosystem do. Is it something like Rails' Rack? Can someone please explain?
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?
650
votes
6
answers
188k
views
Express.js - app.listen vs server.listen
This may be a very basic question but I simply don't get it. What is the difference between creating an app using Express.js and starting the app listening on port 1234, for example:
var express = ...
648
votes
39
answers
393k
views
Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)
I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn't let me write so much code, so I would just say ...
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'...
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 ...
633
votes
10
answers
668k
views
Enabling HTTPS on express.js
I'm trying to get HTTPS working on express.js for node, and I can't figure it out.
This is my app.js code.
var express = require('express');
var fs = require('fs');
var privateKey = fs.readFileSync('...
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?
610
votes
22
answers
1.0m
views
How to set environment variables from within package.json?
How to set some environment variables from within package.json to be used with npm start like commands?
Here's what I currently have in my package.json:
{
...
"scripts": {
"help": "tagove ...
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:
...
605
votes
11
answers
1.1m
views
Proper way to return JSON using node or Express
So, one can attempt to fetch the following JSON object:
$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: ...
600
votes
29
answers
1.8m
views
How can I update npm on Windows?
I tried this:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
...but it didn't work.
How do I do this on Windows?
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:
...
590
votes
2
answers
309k
views
What is the difference between __dirname and ./ in node.js?
When programming in Node.js and referencing files that are located somewhere in relation to your current directory, is there any reason to use the __dirname variable instead of just a regular ./? I've ...
588
votes
8
answers
445k
views
How can I pretty-print JSON using node.js?
This seems like a solved problem but I am unable to find a solution for it.
Basically, I read a JSON file, change a key, and write back the new JSON to the same file. All works, but I loose the JSON ...
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 ...
584
votes
8
answers
414k
views
Node.js check if path is file or directory
I can't seem to get any search results that explain how to do this.
All I want to do is be able to know if a given path is a file or a directory (folder).
582
votes
21
answers
214k
views
ExpressJS How to structure an application?
I'm using the ExpressJS web framework for NodeJS.
People using ExpressJS put their environments (development, production, test...), their routes etc on the app.js. I think that it's not a beautiful ...
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 ...
573
votes
11
answers
699k
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 ...
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 ...
564
votes
10
answers
675k
views
How do you reinstall an app's dependencies using npm?
Is there a simple way to reinstall all packages that my app depends on (i.e. they are in my apps node_modules folder)?
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) {
...
558
votes
52
answers
720k
views
Express.js req.body undefined
I have this as configuration of my Express server
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.set('view engine', 'ejs');
app.set("...
548
votes
35
answers
540k
views
Remove directory which is not empty
In my Node application I need to remove a directory which has some files, but fs.rmdir only works on empty directories. How can I do this?
548
votes
20
answers
1.0m
views
Setting Environment Variables for Node to retrieve
I'm trying to follow a tutorial and it says:
There are a few ways to load credentials.
Loaded from environment variables,
Loaded from a JSON file on disk,
The keys need to be as ...
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 ...
540
votes
8
answers
406k
views
Why does Node.js's fs.readFile() return a buffer instead of string?
I'm trying to read the content of test.txt(which is on the same folder of the Javascript source) and display it using this code:
var fs = require("fs");
fs.readFile("test.txt", function (err, data) {...
540
votes
22
answers
867k
views
How can I change the version of npm using nvm?
I've been using NVM to install the latest versions of Node.js for my Node.js work. It works totally fine for installing separate versions and switching between them. It also installs the latest ...
537
votes
31
answers
768k
views
How do I debug "Error: spawn ENOENT" on node.js?
When I get the following error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:1000:11)
at Process....
531
votes
7
answers
140k
views
Grunt watch error - Waiting...Fatal error: watch ENOSPC
Why do I get the Waiting...Fatal error: watch ENOSPC when I run the watch task ?
How do I solve this issue?
531
votes
18
answers
857k
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: ...
526
votes
4
answers
349k
views
npm command to uninstall or prune unused packages in Node.js
Is there a way to simply uninstall all unused (undeclared) dependencies from a Node.js project (ones that are no longer defined in my package.json.) When I update my application I like to have the ...
525
votes
19
answers
259k
views
Cannot install packages using node package manager in Ubuntu
NodeJS interpreter name(node) on Ubuntu has been renamed to nodejs because of a name conflict with another package. Here's what the readme. Debian says:
The upstream name for the Node.js ...
523
votes
33
answers
626k
views
How to determine a user's IP address in node
How can I determine the IP address of a given request from within a controller? For example (in express):
app.post('/get/ip/address', function (req, res) {
// need access to IP address here
})
523
votes
16
answers
1.0m
views
How to clean node_modules folder of packages that are not in package.json?
Assume I install project packages with npm install that looks into package.json for modules to be installed. After a while I see that I don't need some specific module and remove its dependency from ...
522
votes
3
answers
541k
views
module.exports vs. export default in Node.js and ES6
What is the difference between Node's module.exports and ES6's export default? I'm trying to figure out why I get the "__ is not a constructor" error when I try to export default in Node.js 6.2.2.
...
517
votes
17
answers
403k
views
How can I use async/await at the top level?
I have been going over async/await and after going over several articles, I decided to test things myself. However, I can't seem to wrap my head around why this does not work:
async function main() { ...
509
votes
24
answers
1.5m
views
How to run a shell script at startup
On an Amazon S3 Linux instance, I have two scripts called start_my_app and stop_my_app which start and stop forever (which in turn runs my Node.js application). I use these scripts to manually start ...
509
votes
23
answers
714k
views
How do I update/upsert a document in Mongoose?
Perhaps it's the time, perhaps it's me drowning in sparse documentation and not being able to wrap my head around the concept of updating in Mongoose :)
Here's the deal:
I have a contact schema and ...
507
votes
24
answers
1.4m
views
How to downgrade to a previous Node version
I want to downgrade my Node version from the latest to v6.10.3.
But nothing has worked so far. Tried NVM and it gives an error as well by saying that the make command is not found. How can I downgrade ...
506
votes
10
answers
485k
views
What is Node.js? [closed]
I don't fully get what Node.js is all about. Maybe it's because I am mainly a web based business application developer. What is it and what is the use of it?
My understanding so far is that:
The ...
506
votes
9
answers
577k
views
What does npm install --legacy-peer-deps do exactly? When is it recommended / What's a potential use case?
Just ran into this error:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! ...
505
votes
10
answers
241k
views
What is Express.js? [closed]
I am a learner in Node.js.
What's Express.js?
What's the purpose of it with Node.js?
Why do we actually need Express.js? How is it useful for us to use with Node.js?
What's Redis? Does it come with ...
503
votes
14
answers
765k
views
How to run Node.js as a background process and never die?
I connect to the linux server via putty SSH. I tried to run it as a background process like this:
$ node server.js &
However, after 2.5 hrs the terminal becomes inactive and the process dies. ...