Server-side JavaScript
This guide covers how to install and run NodeJS (aka "Node" or "Node.js").
NodeJS is a way of running JavaScript on the server rather than in the browser.
Where it can run:
- Web (for desktop and mobile browsers)
- Mobile app (e.g. with React Native)
- Desktop app (e.g. with Proton Native or Electron)
Some things you could build:
- Simple scripts for use in the command-line
- Tooling (built, automation, linting, browser tests, web scrapers)
- Games
- Web servers (serve pages similar to Flask or PHP)
- Single-Page Applications (using server-side code to build a front-end app that could be served with a Node server or as static assets)
- APIs - REST API, web sockets and GraphQL APIs. Thesse could have a database behind them
- Shareable libraries
- IoT
Often applications are built with JavaScript on both the backend (server-side) and frontend. This is an isomorphic design. It can mean you don't have to switch languages when working on both and you only need an engineer who can do JavaScript.
Still, JavaScript is not beginner-friendly like Ruby or Python and also those have been doing server-side tasks a lot longer. So just because you can use JavaScript on your backend doesn't mean you should.
Node is single-threaded and so it is great for concurrency and asynchronous tasks. But it is not great at CPU-bound tasks like reading files or machine learning. So Python is a much better alternative for machine learning, leveraging its multiprocessing support (processes across CPUs).
- NodeJS page on Wikipedia.
Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a browser.
- Initial release: 2009
- Repository: nodejs/node
- Node docs
- Node docs - see 3 main sections, listed below.
- API
- ES6 features aka "ECMAScript 2015 (ES6) and beyond"
The ES6 section describes the three ES6 feature groups, and details which features are enabled by default in Node.js, alongside explanatory links. It also shows how to find which version of V8 shipped with a particular Node.js release.
- Guides
The Guides section has long-form, in-depth articles about Node.js technical features and capabilities.
- Node API doc. Also covers the standard lib.
- Node docs - see 3 main sections, listed below.
- Tutorials
- Node.js on W3 Schools
- Node.js Built-in Modules on W3 schools.
- Courses
- Intro to Node.js on Frontend Masters.
Install NodeJS using instructions in this gist.
See also NPM (Node Package Manager) guide.
How to run NodeJS code
Start the interactive console.
$ node
Welcome to Node.js v12.1.0.
Type ".help" for more information.
>
Then enter a command and and press Enter.
For example:
> console.log('Hello!');
Hello!
Press CTRL+D to exit the console.
Use node
command and the path to a script.
For example, create a script.
$ echo 'console.log("Hello!");' > hello.js
Then run it:
$ node hello.js
Create a similar script to above, but using #!/usr/bin/env node
at the start and with executable permissions.
$ echo '#!/usr/bin/env node\nconsole.log("Hello!");' > hello_executable.js
$ chmod +x hello_executable.js
Then run it:
$ ./hello_executable.js
You can run arbitrary code using npm
. If you add a package.json
file in your project (typical for Node projects), then you can add items to the "scripts"
section. These can be any shell command, such as using Bash, Node or NPM to do a task.
Then run them from the command line using one of the approaches below.
Using run-script.
# List available commands.
$ npm run
$ npm run COMMAND
# Longer form
$ npm run-script COMMAND
Using npm-run
$ npm-run COMMAND
For example:
package.json
{
"scripts": {
"foo": "echo Foo!",
"start": "node server",
"build": "echo 'Building site' && echo 'Done!'"
}
}
$ npm run foo
$ npm run start
$ npm run build
NPM also supports this a special commands that doesn't need run
.
$ npm start
If you don't configure start
, then the command above will run this for you:
npm node server.js
See Default values for NPM scripts.
A less common approach is to run an NPM package without going through NPM and using a file in the .bin
directory. This may not be supported for all packages.
For example, after running npm install
, this script could have been added to .bin
with executable permissions.
node_modules/.bin/hello
#!/usr/bin/env node
console.log("Hello, world!");
This can then be run as:
$ node_modules/.bin/hello`
Hello, world!
The bin script is an entry point and might be short, with most of the package contents installed here:
node_modules/
hello/
bin/
src/
# ...
README.md
# ...
How to use the standard lib - builtin packages.
Note: Unfortunately, according a Node.js course I watched, Node.js has a small/limited standard library. This is intentional so that the maintainers can focus on core behavior and security and get out new releases faster, leaving the community to write custom libraries and iterate on those at their own pace. For example, Node.js and also browser JS has no sum
function - you have to write your own or copy one off of the internet or install a library or use one that is within a larger library.
- Node Built-in Modules.
Node.js has a set of built-in modules which you can use without any further installation.
See the full list above. Some are covered below.
var assert = require('assert');
assert(5 > 7);
var fs = require('fs');
fs.readFile('demofile.txt', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});
var path = require('path');
var filename = path.basename('/Users/Refsnes/demo_path.js');
console.log(filename);