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.
node.js
473,471
questions
5393
votes
22
answers
1.9m
views
What's the difference between tilde(~) and caret(^) in package.json?
After I upgraded to the latest stable node and npm, I tried npm install moment --save. It saves the entry in the package.json with the caret ^ prefix. Previously, it was a tilde ~ prefix.
Why are ...
3218
votes
34
answers
2.3m
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 ...
3093
votes
16
answers
1.2m
views
What is the --save option for npm install?
I saw some tutorial where the command was:
npm install --save
What does the --save option mean?
3083
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 ...
3015
votes
32
answers
2.2m
views
Find the version of an installed npm package
How can I find the version of an installed Node.js or npm package?
This prints the version of npm itself:
npm -v <package-name>
This prints a cryptic error:
npm version <package-name>
...
2923
votes
16
answers
1.0m
views
What's the difference between dependencies, devDependencies, and peerDependencies in NPM package.json file?
This documentation answers my question very poorly. I didn't understand those explanations. Can someone say in simpler words? Maybe with examples if it's hard to choose simple words?
Also added ...
2807
votes
42
answers
1.7m
views
How can I update each dependency in package.json to the latest version?
I copied package.json from another project and now want to bump all of the dependencies to their latest versions since this is a fresh project and I don't mind fixing something if it breaks.
What's ...
2481
votes
15
answers
1.1m
views
Do I commit the package-lock.json file created by npm 5?
npm 5 was released today and one of the new features include deterministic installs with the creation of a package-lock.json file.
Is this file supposed to be kept in source control?
I'm assuming it'...
2347
votes
67
answers
3.0m
views
Error message "error:0308010C:digital envelope routines::unsupported"
I created the default IntelliJ IDEA React project and got this:
Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:67:19)
at Object.createHash (...
2327
votes
22
answers
1.2m
views
How to exit in Node.js
What is the command that is used to exit? (i.e terminate the Node.js process)
2191
votes
17
answers
586k
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 ...
2171
votes
67
answers
2.4m
views
How can I update Node.js and npm to their latest versions?
I just installed Node.js and npm. I installed npm for access to additional Node.js modules. After I installed Node.js and npm, I noticed that neither were the latest versions available.
So my ...
2111
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?
1832
votes
42
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, ...
1765
votes
36
answers
2.9m
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 ...
1726
votes
24
answers
1.5m
views
How can the default node version be set using NVM?
I have installed nvm (ubuntu with zsh shell) with two node version: v6.11.5 and v9.0.0 and the default version in nvm is the v9.0.0
Every time I need to change the node version
$ nvm list
v6....
1709
votes
42
answers
553k
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 ...
1622
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?
1597
votes
26
answers
1.9m
views
How to get GET (query string) variables in Express.js on Node.js?
Can we get the variables in the query string in Node.js just like we get them in $_GET in PHP?
I know that in Node.js we can get the URL in the request. Is there a method to get the query string ...
1562
votes
13
answers
528k
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 ...
1560
votes
11
answers
930k
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'].
1517
votes
23
answers
1.3m
views
Where does npm install packages?
Can someone tell me where can I find the Node.js modules, which I installed using npm?
1511
votes
40
answers
703k
views
How to fix npm throwing error without sudo
I just installed node and npm through the package on nodejs.org, and whenever I try to search or install something with npm, it throws the following error unless I sudo the command. I have a feeling ...
1509
votes
21
answers
967k
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",
"...
1505
votes
19
answers
990k
views
Check synchronously if file/directory exists in Node.js
How can I synchronously check, using node.js, if a file or directory exists?
1458
votes
25
answers
1.1m
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 ...
1415
votes
24
answers
1.6m
views
How can I uninstall npm modules in Node.js?
As commonly known, any npm module can be installed by running a simple command: npm install <module_name>.
I have installed a few modules that I do not use any more and I just want to get them ...
1331
votes
15
answers
1.2m
views
How do I get the path to the current script with Node.js?
How would I get the path to the script in Node.js?
I know there's process.cwd, but that only refers to the directory where the script was called, not of the script itself. For instance, say I'm in /...
1321
votes
11
answers
845k
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 ...
1288
votes
35
answers
1.4m
views
Using Node.js as a simple web server
I want to run a very simple HTTP server. Every GET request to example.com should get index.html served to it but as a regular HTML page (i.e., same experience as when you read normal web pages).
...
1268
votes
18
answers
513k
views
What is the difference between --save and --save-dev?
What is the difference between:
npm install [package_name]
and:
npm install [package_name] --save
and:
npm install [package_name] --save-dev
What does this mean? And what is really the effect of --...
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 ...
1251
votes
8
answers
1.1m
views
How can I do Base64 encoding in Node.js?
Does Node.js have built-in Base64 encoding yet?
The reason why I ask this is that final() from crypto can only output hexadecimal, binary or ASCII data. For example:
var cipher = crypto.createCipheriv(...
1234
votes
10
answers
1.2m
views
How to install a previous exact version of a NPM package?
I used nvm to download node v0.4.10 and installed npm to work with that version of node.
I am trying to install express using
npm install express -g
and I get an error that express requires node ...
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....
1221
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:...
1181
votes
25
answers
966k
views
How to create a directory if it doesn't exist using Node.js
Is the following the right way to create a directory if it doesn't exist?
It should have full permission for the script and readable by others.
var dir = __dirname + '/upload';
if (!path.existsSync(...
1156
votes
40
answers
2.2m
views
Upgrading Node.js to the latest version
So, I have Node.js installed and now when I tried to install Mongoosejs, I got an error telling me that I don't have the needed version of Node.js (I have v0.4.11 and v0.4.12 is needed).
How can I ...
1142
votes
24
answers
1.6m
views
How is an HTTP POST request made in node.js?
How can I make an outbound HTTP POST request, with data, in node.js?
1138
votes
12
answers
443k
views
Node.js + Nginx - What now?
I've set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions:
How should they work together? How should I handle the requests?
There are 2 concepts for ...
1109
votes
9
answers
408k
views
Node.js/Windows error: ENOENT, stat 'C:\Users\RT\AppData\Roaming\npm'
I have Windows 7 32-bit. I installed the latest Node.js 32 bit.
When I try to run the command npm install jquery, I receive the error:
Error: ENOENT, stat 'C:\Users\RT\AppData\Roaming\npm
How ...
1044
votes
19
answers
773k
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 ...
1042
votes
32
answers
1.2m
views
Upgrade Node.js to the latest version on Mac OS
Currently I am using Node.js v0.6.16 on Mac OS X 10.7.4. Now I want to upgrade it to the latest Node.js v0.8.1. But after downloading and installing the latest package file from nodejs.org, I found ...
1036
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?
1009
votes
42
answers
641k
views
How to change node.js's console font color?
I had to change the console background color to white because of eye problems, but the font is gray colored and it makes the messages unreadable. How can I change it?
952
votes
12
answers
464k
views
Why does "npm install" rewrite package-lock.json?
I just recently upgraded to npm@5. I now have a package-lock.json file with everything from package.json. I would expect that, when I run npm install that the dependency versions would be pulled ...
938
votes
32
answers
782k
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 ...
936
votes
10
answers
565k
views
Node.js: printing to console without a trailing newline?
Is there a method for printing to the console without a trailing newline? The console object documentation doesn't say anything regarding that:
console.log()
Prints to stdout with newline. This ...
930
votes
34
answers
577k
views
Is there a way to get the version from the 'package.json' file in Node.js code?
Is there a way to get the version set in the package.json file in a Node.js application? I would want something like this
var port = process.env.PORT || 3000
app.listen port
console.log "Express ...
920
votes
24
answers
956k
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" ...