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,728
questions
919
votes
24
answers
948k
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" ...
896
votes
44
answers
1.1m
views
How to fix "ReferenceError: primordials is not defined" in Node.js
I have installed Node.js modules by 'npm install', and then I tried to do gulp sass-watch in a command prompt. After that, I got the below response.
[18:18:32] Requiring external module babel-register
...
893
votes
22
answers
1.0m
views
npm check and update package if needed
We need to integrate Karma test runner into TeamCity and for that I'd like to give sys-engineers small script (powershell or whatever) that would:
pick up desired version number from some config file ...
891
votes
12
answers
768k
views
What is "export default" in JavaScript?
File: SafeString.js
// Build out our basic SafeString type
function SafeString(string) {
this.string = string;
}
SafeString.prototype.toString = function() {
return "" + this.string;
};
...
888
votes
32
answers
935k
views
How can I print a circular structure in a JSON-like format?
I have a big object I want to convert to JSON and send. However it has circular structure, so if I try to use JSON.stringify() I'll get:
TypeError: Converting circular structure to JSON
or
...
884
votes
12
answers
557k
views
Call async/await functions in parallel
As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallel. ...
875
votes
58
answers
1.6m
views
Node / Express: EADDRINUSE, Address already in use - how can I stop the process using the port?
I have a simple server running in node.js using connect:
var server = require('connect').createServer();
//actions...
server.listen(3000);
In my code I have actual route handlers, but that's the ...
871
votes
24
answers
327k
views
How do I convert an existing callback API to promises?
I want to work with promises but I have a callback API in a format like:
1. DOM load or other one time event:
window.onload; // set to callback
...
window.onload = function() {
};
2. Plain ...
866
votes
15
answers
592k
views
How do you prevent install of "devDependencies" NPM modules for Node.js (package.json)?
I have this in my package.json file (shortened version):
{
"name": "a-module",
"version": "0.0.1",
"dependencies": {
"coffee-script": ">= 1.1.3"
},
"devDependencies": {
"...
864
votes
24
answers
350k
views
module.exports vs exports in Node.js
I've found the following contract in a Node.js module:
module.exports = exports = nano = function database_module(cfg) {...}
I wonder what's the difference between module.exports and exports and why ...
859
votes
33
answers
746k
views
Node.js quick file server (static files over HTTP)
Is there Node.js ready-to-use tool (installed with npm), that would help me expose folder content as file server over HTTP.
Example, if I have
D:\Folder\file.zip
D:\Folder\file2.html
D:\Folder\...
855
votes
34
answers
1.1m
views
How do I test a single file using Jest?
I am able to test multiple files using Jest, but I cannot figure out how to test a single file.
I have:
Run npm install jest-cli --save-dev
Updated package.json: `{ ... "scripts": { "...
852
votes
14
answers
774k
views
Using Node.JS, how do I read a JSON file into (server) memory?
Background
I am doing some experimentation with Node.js and would like to read a JSON object, either from a text file or a .js file (which is better??) into memory so that I can access that object ...
851
votes
33
answers
3.0m
views
How do I resolve "Cannot find module" error using Node.js?
After pulling down a module from GitHub and following the instructions to build it, I try pulling it into an existing project using:
> npm install ../faye
This appears to do the trick:
> npm ...
846
votes
33
answers
290k
views
nvm keeps "forgetting" node in new terminal session
Upon using a new terminal session in OS X, nvm forgets the node version and defaults to nothing:
$ nvm ls:
.nvm
v0.11.12
v0.11.13
I have to keep hitting nvm use v.0.11.13 in ...
839
votes
12
answers
849k
views
Execute a command line binary with Node.js
I am in the process of porting a CLI library from Ruby over to Node.js. In my code I execute several third party binaries when necessary. I am not sure how best to accomplish this in Node.
Here's an ...
834
votes
54
answers
1.1m
views
Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object
I am getting this error:
Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
...
829
votes
11
answers
595k
views
Node.js Best Practice Exception Handling
I just started trying out node.js a few days ago. I've realized that the Node is terminated whenever I have an unhandled exception in my program. This is different than the normal server container ...
828
votes
48
answers
770k
views
Babel 6 regeneratorRuntime is not defined
I'm trying to use async/await from scratch on Babel 6, but I'm getting regeneratorRuntime is not defined.
.babelrc file
{
"presets": [ "es2015", "stage-0" ]
}
...
818
votes
26
answers
900k
views
node.js remove file
How do I delete a file with node.js?
http://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback
I don't see a remove command?
809
votes
31
answers
755k
views
Error: request entity too large
I'm receiving the following error with express:
Error: request entity too large
at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/...
773
votes
35
answers
1.1m
views
MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
I can't make a simple connection to the server for some reason. I install the newest MySQL Community 8.0 database along with Node.JS with default settings.
This is my node.js code
var mysql = ...
769
votes
27
answers
1.4m
views
How can I wait In Node.js (JavaScript)? l need to pause for a period of time
I'm developing a console script for personal needs. I need to be able to pause for an extended amount of time, but, from my research, Node.js has no way to stop as required. It’s getting hard to read ...
769
votes
31
answers
731k
views
Read a file one line at a time in node.js?
I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together.
var Lazy=require("...
767
votes
37
answers
894k
views
Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?
I am trying to support CORS in my Node.js application that uses the Express.js web framework. I have read a Google group discussion about how to handle this, and read a few articles about how CORS ...
767
votes
18
answers
831k
views
How to append to a file in Node?
I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.
fs.writeFile('log.txt', 'Hello Node', function (err) {
if (err) throw err;...
764
votes
7
answers
556k
views
How can I specify the required Node.js version in package.json?
I have a Node.js project that requires Node version 12 or higher. Is there a way to specify this in the packages.json file, so that the installer will automatically check and inform the users if they ...
762
votes
30
answers
1.3m
views
How to process POST data in Node.js?
How do you extract form data (form[method="post"]) and file uploads sent from the HTTP POST method in Node.js?
I've read the documentation, googled and found nothing.
function (request, response) {
...
742
votes
30
answers
3.7m
views
How do I update Node.js?
I did the following to update my npm:
npm update npm -g
But I have no idea how to update Node.js. Any suggestions? (I'm using Node.js 0.4.1 and want to update to Node.js 0.6.1.)
739
votes
20
answers
958k
views
How to run TypeScript files from command line?
I'm having a surprisingly hard time finding an answer to this. With plain Node.JS, you can run any js file with node path/to/file.js, with CoffeeScript it's coffee hello.coffee and ES6 has babel-node ...
737
votes
37
answers
1.3m
views
Node Version Manager install - nvm command not found
I am trying to install NVM as per these instructions
I typed in this command in terminal:
$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh
After running the install, I restart the ...
734
votes
43
answers
908k
views
Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
What might be causing the error Error: EACCES: permission denied, access '/usr/local/lib/node_modules'?
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! ...
734
votes
25
answers
473k
views
How to use executables from a package installed locally in node_modules?
How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:
npm install coffee-script
This installs it in ./node_modules and the coffee command is in ./...
726
votes
20
answers
738k
views
Local dependency in package.json
I want to do something like this, so npm install also installs the package.json of ../somelocallib or more importantly its dependencies.
"dependencies": {
"express": "*&...
721
votes
63
answers
522k
views
Node Sass couldn't find a binding for your current environment
I am having issues building an app because node-sass keeps failing with the error.
ERROR in Missing binding /Users/warren/Sites/random-docs/my-cms/node_modules/node-sass/vendor/darwin-x64-11/...
718
votes
30
answers
600k
views
How to store Node.js deployment settings/configuration files?
I have been working on a few Node apps, and I've been looking for a good pattern of storing deployment-related settings. In the Django world (where I come from), the common practise would be to have a ...
718
votes
8
answers
1.1m
views
What is "require" in JavaScript and NodeJS?
I'm trying to get JavaScript to read/write to a PostgreSQL database. I found this project on GitHub. I was able to get the following sample code to run in Node.
var pg = require('pg'); //native libpq ...
717
votes
9
answers
240k
views
How, in general, does Node.js handle 10,000 concurrent requests?
I understand that Node.js uses a single-thread and an event loop to process requests only processing one at a time (which is non-blocking). But still, how does that work, lets say 10,000 concurrent ...
717
votes
10
answers
1.4m
views
Node.js version on the command line? (not the REPL)
I want to get the version of Node.js on the command line. I'm expecting to run a command like:
node -version
but that doesn't work. Does anybody know what the command line would be? (i.e. not the ...
708
votes
17
answers
269k
views
Node.js on multi-core machines
Node.js looks interesting, BUT I must miss something - isn't Node.js tuned only to run on a single process and thread?
Then how does it scale for multi-core CPUs and multi-CPU servers? After all, it ...
695
votes
12
answers
336k
views
Differences between socket.io and websockets
What are the differences between socket.io and websockets in
node.js?
Are they both server push technologies?
The only differences I felt was,
socket.io allowed me to send/emit messages by ...
693
votes
10
answers
755k
views
How to access the GET parameters after "?" in Express?
I know how to get the params for queries like this:
app.get('/sample/:id', routes.sample);
In this case, I can use req.params.id to get the parameter (e.g. 2 in /sample/2).
However, for url like /...
690
votes
25
answers
1.8m
views
How to change to an older version of Node.js
I am running Node.js version v0.5.9-pre on Ubuntu 10.10.
I would like to be using version v0.5.0-pre.
How do I roll back to the older version of node?
690
votes
21
answers
797k
views
How to get the full URL in Express?
Let's say my sample URL is
http://example.com/one/two
and I say I have the following route
app.get('/one/two', function (req, res) {
var url = req.url;
}
The value of url will be /one/two.
How ...
689
votes
13
answers
478k
views
bodyParser is deprecated express 4
I am using express 4.0 and I'm aware that body parser has been taken out of the express core, I am using the recommended replacement, however I am getting
body-parser deprecated bodyParser: use ...
681
votes
46
answers
455k
views
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac
I installed node using homebrew (Mojave), afterwards php stoped working and if I try to run php -v I get this error:
php -v
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib
...
673
votes
11
answers
552k
views
Installing a local module using npm?
I have a downloaded module repo, I want to install it locally, not globally in another directory?
What is an easy way to do this?
669
votes
33
answers
882k
views
How to download a file with Node.js (without using third-party libraries)?
How do I download a file with Node.js without using third-party libraries?
I don't need anything special. I only want to download a file from a given URL, and then save it to a given directory.
668
votes
33
answers
1.7m
views
SyntaxError: Cannot use import statement outside a module
I've got an ApolloServer project that's giving me trouble, so I thought I might update it and ran into issues when using the latest Babel. My "index.js" is:
require('dotenv').config()
import ...
667
votes
12
answers
492k
views
How do I override nested NPM dependency versions?
I would like to use the grunt-contrib-jasmine NPM package. It has various dependencies. Part of the dependency graph looks like this:
─┬ [email protected]
│ ├─┬ [email protected]
│...