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
417 votes
21 answers
311k views

Get all directories within directory nodejs

I was hoping this would be a simple thing, but I cannot find anything out there to do so. I just want to get all folders/directories within a given folder/directory. So for example: <MyFolder>...
Grofit's user avatar
  • 18k
417 votes
7 answers
411k views

Is there a way to force npm to generate package-lock.json?

I deleted it by accident and have made many changes to package.json since. An npm install or npm update do not generate package-lock.json anymore. I tried clearing my npm cache and my nvm cache, but ...
Big Money's user avatar
  • 9,658
416 votes
16 answers
505k views

Express.js: how to get remote client address

I don't completely understand how I should get a remote user IP address. Let's say I have a simple request route such as: app.get(/, function (req, res){ var forwardedIpsStr = req.header('x-...
Erik's user avatar
  • 14.4k
415 votes
6 answers
688k views

How to extract request http headers from a request using NodeJS connect

I'd like to get the "Host" header of a request made using Node JS's connect library bundle. My code looks like: var app = connect() .use(connect.logger('dev')) .use(connect.static('public')) ....
Alex Spurling's user avatar
415 votes
10 answers
275k views

How do I determine the current operating system with Node.js

I'm writing a couple of node shell scripts for use when developing on a platform. We have both Mac and Windows developers. Is there a variable I can check for in Node to run a .sh file in one instance ...
Mauvis Ledford's user avatar
414 votes
23 answers
707k views

Can you force Vue.js to reload/re-render?

Just a quick question. Can you force Vue.js to reload/recalculate everything? If so, how?
Dave's user avatar
  • 12.3k
414 votes
9 answers
550k views

How to create an HTTPS server in Node.js?

Given an SSL key and certificate, how does one create an HTTPS service?
murvinlai's user avatar
  • 49.7k
412 votes
31 answers
366k views

"NODE_ENV" is not recognized as an internal or external command, operable command or batch file

I'm trying to setup an environment for a Node.js app. but I'm getting this error every time. "NODE_ENV" is not recognized as an internal or external command, operable command or batch file. ...
krozero's user avatar
  • 6,197
410 votes
15 answers
257k views

How to run Gulp tasks sequentially one after the other

in the snippet like this: gulp.task "coffee", -> gulp.src("src/server/**/*.coffee") .pipe(coffee {bare: true}).on("error",gutil.log) .pipe(gulp.dest "bin") gulp.task "clean",-...
iLemming's user avatar
  • 35.4k
406 votes
19 answers
356k views

Node.js: what is ENOSPC error and how to solve?

I have a problem with Node.js and uploading files to server. For uploading files to server I use this plugin. When starting file upload to the server, Node.js process crashed and show error: Error: ...
Giffo's user avatar
  • 4,191
404 votes
19 answers
1.1m views

Node.js getaddrinfo ENOTFOUND

When using Node.js to try and get the html content of the following web page: eternagame.wikia.com/wiki/EteRNA_Dictionary I get the following error: events.js:72 throw er; // Unhandled 'error' ...
Vineet Kosaraju's user avatar
403 votes
12 answers
196k views

Should I check in folder "node_modules" to Git when creating a Node.js app on Heroku?

I followed the basic getting started instructions for Node.js on Heroku here: https://devcenter.heroku.com/categories/nodejs These instructions don't tell you to create a .gitignore node_modules, and ...
Jason Griffin's user avatar
403 votes
5 answers
322k views

How to list all versions of an npm module?

In order to see all the versions of a node module [webpack], I have executed below command in windows command prompt npm view webpack versions This command only displays first 100 versions and later ...
refactor's user avatar
  • 14.5k
402 votes
10 answers
288k views

Call a "local" function within module.exports from another function in module.exports?

How do you call a function from within another function in a module.exports declaration? app.js var bla = require('./bla.js'); console.log(bla.bar()); bla.js module.exports = { foo: function (...
k00k's user avatar
  • 17.5k
402 votes
7 answers
320k views

Folder structure for a Node.js project

I notice that Node.js projects often include folders like these: /libs, /vendor, /support, /spec, /tests What exactly do these mean? What's the different between them, and where should I include ...
guilin 桂林's user avatar
401 votes
39 answers
517k views

NPM Install Error:Unexpected end of JSON input while parsing near '...nt-webpack-plugin":"0'

When creating a new Angular 5 project: node version: 8.9.2 npm version: 5.5.1 My Command is: npm install -g @angular/cli The Error is: npm ERR! **Unexpected end of JSON input while parsing near '......
kathir's user avatar
  • 4,275
401 votes
9 answers
124k views

Differences between express.Router and app.get?

I'm starting with NodeJS and Express 4, and I'm a bit confused. I have been reading the Express website, but can't see when to use a route handler or when to use express.Router. As I could see if I ...
nelson687's user avatar
  • 4,463
399 votes
8 answers
66k views

Detect if called through require or directly by command line

How can I detect whether my Node.JS file was called using SH:node path-to-file or JS:require('path-to-file')? This is the Node.JS equivalent to my previous question in Perl: How can I run my Perl ...
700 Software's user avatar
  • 86.5k
398 votes
5 answers
536k views

Is there a compatibility list for Angular / Angular-CLI and Node.js?

I periodically run into the problem, having to spin up old Angular projects with deprecated dependencies of Angular. Because I unsually run the latest Node.js version (at least lates LTS version) I ...
jowey's user avatar
  • 8,001
398 votes
10 answers
298k views

Apache and Node.js on the Same Server

I want to use Node because it's swift, uses the same language I am using on the client side, and it's non-blocking by definition. But the guy who I hired to write the program for file handling (...
Matt's user avatar
  • 5,613
397 votes
46 answers
809k views

Node.js Port 3000 already in use but it actually isn't?

I have been working with a node.js project for a few weeks and it has been working great. Usually, I use npm start to run my app and view it in a browser on localhost, port 3000. Today, I started to ...
user2573690's user avatar
  • 5,743
396 votes
49 answers
431k views

Node.js fs.readdir recursive directory search

Any ideas on an async directory search using fs.readdir? I realize that we could introduce recursion and call the read directory function with the next directory to read, but I'm a little worried ...
crawf's user avatar
  • 9,548
396 votes
18 answers
196k views

node.js require() cache - possible to invalidate?

From the node.js documentation: Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, ...
William's user avatar
  • 13.5k
395 votes
8 answers
643k views

How to install npm peer dependencies automatically?

For example, when I install Angular2: npm install --save angular2 [email protected] /Users/doug/Projects/dougludlow/temp ├── [email protected] ├── UNMET PEER DEPENDENCY es6-promise@^3.0.2 ├── UNMET ...
Douglas Ludlow's user avatar
394 votes
19 answers
301k views

Why does JSHint throw a warning if I am using const?

This is the error I get when using const: <error line="2" column="1" severity="warning" message="&apos;const&apos; is available in ES6 (use esnext option) or Mozilla JS extensions (use moz)...
Andre Schlesinger's user avatar
394 votes
25 answers
653k views

possible EventEmitter memory leak detected

I am getting following warning: (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace: at EventEmitter.<...
Riz's user avatar
  • 10.1k
393 votes
15 answers
370k views

node.js require all files in a folder?

How do I require all files in a folder in node.js? need something like: files.forEach(function (v,k){ // require routes require('./routes/'+v); }};
Harry's user avatar
  • 54.1k
393 votes
24 answers
175k views

`npm install` fails on node-gyp rebuild with `gyp: No Xcode or CLT version detected!`

Every time I try npm install. I get the following error. How do I fix it? gyp: No Xcode or CLT version detected! I am on node -v → v8.8.0 & npm -v → v6.11.3 I tried to run it on VSCode terminal ...
Jeena's user avatar
  • 4,263
391 votes
43 answers
2.0m views

'npm' is not recognized as internal or external command, operable program or batch file

I am completely new to nodejs. I am trying to get nodejs to work on my Windows 2008 box in order to install Karma which I would use for TDDing my AngularJs code. I have done the following steps so far ...
Suhas's user avatar
  • 8,169
391 votes
1 answer
478k views

NodeJS: How to decode base64 encoded string back to binary? [duplicate]

I was implementing password hashing with salt, so I generated salt as binary, hashed the password, base64 encoded the password and salt then stored them into database. Now when I am checking password,...
Xavier_Ex's user avatar
  • 8,620
390 votes
22 answers
241k views

nodejs vs node on ubuntu 12.04

I installed nodejs on ubuntu from instructions given here When I write node --version in the terminal I see this : -bash: /usr/sbin/node: No such file or directory I can see node in the /usr/sbin/ ...
Jatin's user avatar
  • 14.2k
389 votes
18 answers
242k views

Const in JavaScript: when to use it and is it necessary?

I've recently come across the const keyword in JavaScript. From what I can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in Node.js): const x = ...
axdg's user avatar
  • 4,035
388 votes
16 answers
269k views

How to install a private NPM module without my own registry?

I've taken some shared code and put it in an NPM module, one I don't want to upload to the central registry. The question is, how do I install it from other projects? The obvious way is probably to ...
futlib's user avatar
  • 8,338
387 votes
8 answers
128k views

setImmediate vs. nextTick

Node.js version 0.10 was released today and introduced setImmediate. The API changes documentation suggests using it when doing recursive nextTick calls. From what MDN says it seems very similar to ...
Benjamin Gruenbaum's user avatar
387 votes
6 answers
227k views

What is the parameter "next" used for in Express?

Suppose you have a simple block of code like this: app.get('/', function(req, res){ res.send('Hello World'); }); This function has two parameters, req and res, which represent the request and ...
Menztrual's user avatar
  • 41.3k
384 votes
9 answers
318k views

MySQL with Node.js

I've just started getting into Node.js. I come from a PHP background, so I'm fairly used to using MySQL for all my database needs. How can I use MySQL with Node.js?
crawf's user avatar
  • 9,548
381 votes
38 answers
512k views

Node.js EACCES error when listening on most ports

I'm testing out an app (hopefully to run on heroku, but am having issues locally as well). It's giving me an EACCES error when it runs http.Server.listen() - but it only occurs on some ports. So, ...
jwegner's user avatar
  • 7,243
379 votes
21 answers
401k views

Eslint: How to disable "unexpected console statement" in Node.js?

I'm using eslint with Sublime Text 3 and I am writing gulpfile.js. /*eslint-env node*/ var gulp = require('gulp'); gulp.task('default', function(){ console.log('default task'); }); But eslint ...
Jean Y.C. Yang's user avatar
379 votes
30 answers
1.4m views

sudo: npm: command not found

I'm trying to upgrade to the latest version of node. I'm following the instructions at http://davidwalsh.name/upgrade-nodejs But when I do: sudo npm install -g n I get the error: sudo: npm: command ...
Mark's user avatar
  • 4,808
379 votes
4 answers
202k views

Code coverage with Mocha

I am using Mocha for testing my NodeJS application. I am not able to figure out how to use its code coverage feature. I tried googling it but did not find any proper tutorial. Please help.
tusharmath's user avatar
  • 10.8k
379 votes
11 answers
158k views

Is it possible to pass a flag to Gulp to have it run tasks in different ways?

Normally in Gulp tasks look like this: gulp.task('my-task', function() { return gulp.src(options.SCSS_SOURCE) .pipe(sass({style:'nested'})) .pipe(autoprefixer('last 10 version')) ...
asolberg's user avatar
  • 6,778
378 votes
14 answers
469k views

npm install won't install devDependencies

On windows for some reason when I run npm install it won't install devDependencies. AFAIK it should. If I run npm install --dev devDependencies are installed. I don't understand why npm install doesn'...
tristantzara's user avatar
  • 5,737
377 votes
16 answers
335k views

Secure random token in Node.js

In this question Erik needs to generate a secure random token in Node.js. There's the method crypto.randomBytes that generates a random Buffer. However, the base64 encoding in node is not url-safe, it ...
Hubert OG's user avatar
  • 19.5k
377 votes
17 answers
1.2m views

How to run a hello.js file in Node.js on windows?

I am trying to run a hello world program written in javascript in a separate file named hello.js Currently running windows version of node.js. The code runs perfectly in console window but how do I ...
Mitul's user avatar
  • 9,784
374 votes
17 answers
552k views

No 'Access-Control-Allow-Origin' - Node / Apache Port Issue

i've created a small API using Node/Express and trying to pull data using Angularjs but as my html page is running under apache on localhost:8888 and node API is listen on port 3000, i am getting the ...
user1336103's user avatar
  • 3,859
372 votes
25 answers
552k views

How to generate unique ID with node.js

function generate(count) { var founded = false, _sym = 'abcdefghijklmnopqrstuvwxyz1234567890', str = ''; while(!founded) { for(var i = 0; i < count; i++) { ...
owl's user avatar
  • 4,371
371 votes
5 answers
179k views

What's the difference between path.resolve and path.join?

Is there some difference between the following invocations? path.join(__dirname, 'app') vs. path.resolve(__dirname, 'app') Which one should be preferred?
helpermethod's user avatar
  • 60.8k
368 votes
17 answers
779k views

Get data from fs.readFile [duplicate]

var content; fs.readFile('./Index.html', function read(err, data) { if (err) { throw err; } content = data; }); console.log(content); Logs undefined, why?
karaxuna's user avatar
  • 26.8k
367 votes
24 answers
595k views

E11000 duplicate key error index in mongodb mongoose

Following is my user schema in user.js model - var userSchema = new mongoose.Schema({ local: { name: { type: String }, email : { type: String, require: true, unique: true }, ...
Trialcoder's user avatar
  • 5,926
366 votes
39 answers
546k views

How to Delete node_modules - Deep Nested Folder in Windows

Upon trying to remove the node_modules directory created by npm install: The source file name(s) are larger than is supported by the file system. Try moving to a location which has a shorter ...
leoh's user avatar
  • 10.5k

1
3 4
5
6 7
9450