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
501 votes
34 answers
661k views

Determine project root from a running node.js application

Is there a different way, other than process.cwd(), to get the pathname of the current project's root-directory. Does Node implement something like ruby's property, Rails.root,. I'm looking for ...
MrEvil's user avatar
  • 7,995
501 votes
12 answers
782k 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,609
500 votes
50 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
500 votes
11 answers
456k views

What does body-parser do with express?

I don't understand why we need body-parser in an Express application, as we can get data without using body-parser. And what does it do actually and how?
jiten's user avatar
  • 5,145
499 votes
14 answers
409k views

node.js hash string?

I have a string that I want to hash. What's the easiest way to generate the hash in node.js? The hash is for versioning, not security.
Harry's user avatar
  • 54.1k
490 votes
6 answers
337k views

How can I get npm start at a different directory?

I usually cd into the app directory and then run npm start. It is my feeling that there ought to be some way to run npm start with a path parameter. But, the npm start documentation contains no such ...
Blaise's user avatar
  • 21.8k
487 votes
10 answers
469k views

How can I mock an ES6 module import using Jest?

I want to test that one of my ES6 modules calls another ES6 module in a particular way. With Jasmine this is super easy -- The application code: // myModule.js import dependency from './dependency'; ...
Cam Jackson's user avatar
  • 12.1k
485 votes
7 answers
198k views

Understanding passport serialize deserialize

How would you explain the workflow of Passport's serialize and deserialize methods to a layman. Where does user.id go after passport.serializeUser has been called? We are calling passport....
Anubhav's user avatar
  • 7,188
481 votes
6 answers
217k views

How to change bower's default components folder?

I'm making a new project that uses bower from twitter. I created a component.json to maintain all my dependency like jquery. Then I run bower install that installs everything in a folder named ...
Adam Ramadhan's user avatar
480 votes
32 answers
628k views

Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts

When I try to start my app on Heroku I got the following stack trace. It is just a basic ts.app like you see with ts-node and nodemon. I am really interested in what the answer is going to be. 2020-05-...
Elizar Pongracz's user avatar
480 votes
9 answers
1.0m 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
480 votes
9 answers
549k views

How to specify local modules as npm package dependencies

I have an application which has the usual set of dependencies on third party modules (e.g. "express") specified in the package.json file under dependencies. E.g. "express" : "...
Sam Adams's user avatar
  • 5,537
478 votes
26 answers
617k views

How can I set NODE_ENV=production on Windows?

In Ubuntu it's quite simple; I can run the application using: $ NODE_ENV=production node myapp/app.js However, this doesn't work on Windows. Is there a configuration file where I can set the ...
Jack's user avatar
  • 15.8k
470 votes
13 answers
384k views

How do I measure the execution time of JavaScript code with callbacks?

I have a piece of JavaScript code that I am executing using the node.js interpreter. for(var i = 1; i < LIMIT; i++) { var user = { id: i, name: "MongoUser [" + i + "]" }; db.users....
Stormshadow's user avatar
  • 7,019
468 votes
12 answers
335k views

Get file name from absolute path in Nodejs?

How can I get the file name from an absolute path in Nodejs? e.g. "foo.txt" from "/var/www/foo.txt" I know it works with a string operation, like fullpath.replace(/.+\//, ''), but ...
fxp's user avatar
  • 6,952
468 votes
12 answers
372k views

Difference between "process.stdout.write" and "console.log" in node.js?

What is the difference between "process.stdout.write" and "console.log" in node.js? EDIT: Using console.log for a variable showed a lot of unreadable characters while using process.stdout.write ...
ajsie's user avatar
  • 78.8k
467 votes
31 answers
609k views

Command to remove all npm modules globally

Is there a command to remove all global npm modules? If not, what do you suggest?
EhevuTov's user avatar
  • 20.4k
461 votes
13 answers
441k views

How to properly upgrade node using nvm

Is it possible to upgrade node right in place, instead of manually installing the latest stable version? I have installed node.js version 5.0 with nvm, but now I want to update it to 5.4. I'm trying ...
Boris Burkov's user avatar
459 votes
22 answers
307k views

Can an AWS Lambda function call another

I have 2 Lambda functions - one that produces a quote and one that turns a quote into an order. I'd like the Order lambda function to call the Quote function to regenerate the quote, rather than just ...
Silver's user avatar
  • 4,981
459 votes
32 answers
374k views

NodeJS / Express: what is "app.use"?

In the docs for the NodeJS express module, the example code has app.use(...). What is the use function and where is it defined?
Alexander Bird's user avatar
458 votes
33 answers
978k views

NodeJS - What does "socket hang up" actually mean?

I'm building a web scraper with Node and Cheerio, and for a certain website I'm getting the following error (it only happens on this one website, no others that I try to scrape. It happens at a ...
JVG's user avatar
  • 20.7k
456 votes
62 answers
683k views

npm ERR cb() never called

I have a Node.js app hosted on Heroku. Every time I do a git push heroku I get the following error: Counting objects: 14, done. Delta compression using up to 6 threads. Compressing objects: 100% (12/...
Louise K's user avatar
  • 4,771
455 votes
13 answers
271k views

Doing a cleanup action just before Node.js exits

I want to tell Node.js to always do something just before it exits, for whatever reason — Ctrl+C, an exception, or any other reason. I tried this: process.on('exit', function (){ console.log('...
Erel Segal-Halevi's user avatar
455 votes
3 answers
316k views

What is the difference between .js and .mjs files?

I have started working on an existing project based on Node.js. I was just trying to understand the flow of execution, where I encountered with some *.mjs files. I have searched the web where I found ...
Keval Bhogayata's user avatar
454 votes
6 answers
439k views

How do I URl encode something in Node.js?

I want to URL encode this: SELECT name FROM user WHERE uid = me() Do I have to download a module for this? I already have the request module.
TIMEX's user avatar
  • 266k
453 votes
3 answers
248k views

Which websocket library to use with Node.js? [closed]

Currently there is a plethora of websocket libraries for node.js, the most popular seem to be: https://github.com/Worlize/WebSocket-Node https://github.com/einaros/ws https://github.com/LearnBoost/...
balupton's user avatar
  • 47.8k
452 votes
13 answers
359k views

Is there a way to automatically build the package.json file for Node.js projects

Is package.json supposed to be manually edited? Couldn't a program like npm just look through the files, see the "require" statements, and then use that to put the necessary entries in the package....
neuromancer's user avatar
  • 54.7k
452 votes
7 answers
642k views

Download a file from NodeJS Server using Express

How can I download a file that is in my server to my machine accessing a page in a nodeJS server? I'm using the ExpressJS and I've been trying this: app.get('/download', function(req, res){ var ...
Thiago Miranda de Oliveira's user avatar
448 votes
38 answers
748k views

Node.js: Python not found exception due to node-sass and node-gyp

Suddenly in one of my jenkins environment build has started failing, while in local machine it seems to be working fine as i have python installed, From the logs i was able to detect that the problem ...
Vipul Panth's user avatar
  • 5,571
448 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 ...
447 votes
21 answers
523k views

Declare multiple module.exports in Node.js

What I'm trying to achieve is to create one module that contains multiple functions in it. module.js: module.exports = function(firstParam) { console.log("You did it"); }, module.exports = function(...
Ali's user avatar
  • 10.3k
443 votes
12 answers
367k views

JavaScript, Node.js: is Array.forEach asynchronous?

I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave asynchronously? For example, if I call: [many many elements].forEach(function () {lots of work to do})...
R. Gr.'s user avatar
  • 4,471
440 votes
4 answers
674k views

How to get a URL parameter in Express?

I am facing an issue on getting the value of tagid from my URL: localhost:8888/p?tagid=1234. Help me out to correct my controller code. I am not able to get the tagid value. My code is as follows: ...
user2834795's user avatar
  • 4,443
440 votes
4 answers
200k views

What's the difference between process.cwd() vs __dirname?

What's the difference between console.log(process.cwd()) and console.log(__dirname); I've seen both used in similar contexts.
helpermethod's user avatar
  • 60.8k
437 votes
13 answers
353k views

Node.js throws "btoa is not defined" error

In my node.js application I did an npm install btoa-atob so that I could use the btoa() and atob() functions which are native in client-side javascript but for some reason weren't included in node. ...
Joey's user avatar
  • 10.8k
436 votes
45 answers
595k views

Get local IP address in Node.js

I have a simple Node.js program running on my machine and I want to get the local IP address of a PC on which my program is running. How do I get it with Node.js?
yojimbo87's user avatar
  • 67k
436 votes
31 answers
511k views

req.body empty on posts

All of a sudden this has been happening to all my projects. Whenever I make a post in nodejs using express and body-parser req.body is an empty object. var express = require('express') var ...
Joseph Dailey's user avatar
436 votes
9 answers
253k views

What is the "__v" field in Mongoose

I'm using Mongoose version 3 with MongoDB version 2.2. I've noticed a __v field has started appearing in my MongoDB documents. Is it something to do with versioning? How is it used?
Simon Lomax's user avatar
  • 8,862
435 votes
34 answers
1.5m views

Start script missing error when running npm start

I'm receiving this error when trying to debug my node application using the npm start command. Error: npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "...
Andrew Moll's user avatar
  • 4,973
434 votes
10 answers
264k views

npm - how to show the latest version of a package

How do I use npm to show the latest version of a module? I am expecting something like npm --latest express to print out v3.0.0.
Trantor Liu's user avatar
  • 8,980
432 votes
17 answers
753k views

How to set NODE_ENV to production/development in OS X

For use in express.js environments. Any suggestions?
Mark Nguyen's user avatar
  • 7,348
431 votes
17 answers
464k views

Mongoose: findOneAndUpdate doesn't return updated document

Below is my code var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var Cat = mongoose.model('Cat', { name: String, age: {type: Number, default: 20}, ...
Dreams's user avatar
  • 8,438
427 votes
5 answers
519k views

Node update a specific package

I want to update my Browser-sync without updating all my node packages. How can I achieve this? My current version of Browser-sync does not have the Browser-sync GUI :( ├─┬ [email protected] │ ├── ...
Samuel's user avatar
  • 5,769
426 votes
23 answers
570k views

Can pm2 run an 'npm start' script

Is there a way for pm2 to run an npm start script or do you just have to run pm2 start app.js So in development npm start Then in production with pm2 you would run something like pm2 start 'npm start'...
svnm's user avatar
  • 23.7k
426 votes
13 answers
639k views

Ignore invalid self-signed ssl certificate in node.js with https.request?

I'm working on a little app that logs into my local wireless router (Linksys) but I'm running into a problem with the router's self-signed ssl certificate. I ran wget 192.168.1.1 and get: ERROR: ...
Geuis's user avatar
  • 41.7k
426 votes
17 answers
320k views

Why does npm install say I have unmet dependencies?

I have a node package. When I run npm install from the package root, it installs a bunch of things, but then prints several error messages that look like this: npm WARN unmet dependency /Users/...
Sean Mackesey's user avatar
425 votes
8 answers
679k views

How do I consume the JSON POST data in an Express application

I'm sending the following JSON string to my server. ( { id = 1; name = foo; }, { id = 2; name = bar; } ) On the server I have this. app.post('...
neuromancer's user avatar
  • 54.7k
424 votes
22 answers
402k views

Async/Await Class Constructor

At the moment, I'm attempting to use async/await within a class constructor function. This is so that I can get a custom e-mail tag for an Electron project I'm working on. customElements.define('e-...
Alexander Craggs's user avatar
424 votes
37 answers
616k views

How to Polyfill node core modules in webpack 5

webpack 5 no longer do auto-polyfilling for node core modules. How to fix it please? BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer ...
Saber's user avatar
  • 4,331
421 votes
20 answers
625k views

receiving error: 'Error: SSL Error: SELF_SIGNED_CERT_IN_CHAIN' while using npm

I am using npm v1.0.104/node 0.6.12 on ubuntu - I am receiving the error copied below while attempting to install any new modules via npm (I tested socket.io earlier using http, not https though & ...
ali haider's user avatar
  • 19.7k