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.

Filter by
Sorted by
Tagged with
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, ...
Peter Kruithof's user avatar
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?
Borek Bernard's user avatar
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 ...
Josh C's user avatar
  • 7,961
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?
tillda's user avatar
  • 18.5k
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?
mike.toString's user avatar
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 = ...
Tamas's user avatar
  • 11.1k
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 ...
user428900's user avatar
  • 7,141
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'...
funseiki's user avatar
  • 9,307
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 ...
JerryKur's user avatar
  • 7,409
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('...
Alan's user avatar
  • 46.4k
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?
bonbonez's user avatar
  • 6,808
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?
John's user avatar
  • 6,197
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 ...
dev.meghraj's user avatar
  • 8,840
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: ...
echox's user avatar
  • 9,816
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: ...
MightyMouse's user avatar
  • 13.4k
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?
Jatin's user avatar
  • 14.2k
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: ...
Jay's user avatar
  • 19.5k
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 ...
thisissami's user avatar
  • 15.9k
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 ...
Rajat's user avatar
  • 33.6k
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 ...
austinthemassive's user avatar
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).
ThomasReggi's user avatar
  • 57.4k
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 ...
Sandro Munda's user avatar
  • 40.4k
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 ...
disc0dancer's user avatar
  • 9,265
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 ...
Jonathan002's user avatar
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 ...
jasa1704's user avatar
  • 5,799
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)?
trusktr's user avatar
  • 45.8k
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/...
Lapsio's user avatar
  • 6,734
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) { ...
alexs333's user avatar
  • 12.4k
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("...
Masiar's user avatar
  • 20.9k
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?
sachin's user avatar
  • 14.1k
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 ...
user1107173's user avatar
  • 10.5k
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 ...
Danny Fox's user avatar
  • 39.8k
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) {...
Nathan Campos's user avatar
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 ...
thisissami's user avatar
  • 15.9k
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....
laconbass's user avatar
  • 17.4k
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?
kds's user avatar
  • 28.4k
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: ...
SofDroid's user avatar
  • 5,500
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 ...
Tarion's user avatar
  • 16.8k
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 ...
Sayem's user avatar
  • 6,099
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 })
Shamoon's user avatar
  • 42.4k
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 ...
Sergei Basharov's user avatar
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. ...
Marty Chang's user avatar
  • 6,449
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() { ...
Felipe's user avatar
  • 11.5k
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 ...
meetamit's user avatar
  • 24.9k
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 ...
Traveling Tech Guy's user avatar
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 ...
Shashika Virajh's user avatar
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 ...
Jeff's user avatar
  • 13.1k
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! ...
antonwilhelm's user avatar
  • 6,650
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 ...
vinod's user avatar
  • 8,410
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. ...
murvinlai's user avatar
  • 49.7k