"Node.js" is several things:
require()
, node_modules
)npm
)"Core is small. Userland is large."
Keep this in mind while writing code in the node.js ecosystem. The small number of core modules and the multiplicity of 3rd-party packages is node.js' solution to the standard library problem: "The standard library is where modules go to die."
In languages with a standard library, frequently the core modules become bloated, stagnated and are crippled by being coupled to the language version and the sheer amount of code that depends on them. The node.js project takes a different approach by focusing on only providing IO-bindings and a very small handful of utility libraries to increase operability (e.g., Stream
, EventEmitter
, util
).
There are 3 defining characteristics that make node.js unique:
Typically though, it's the asynchronous (non-blocking) IO that is the focus when discussing node.js. This makes node.js an excellent choice for IO-bound tasks, but a poor choice for CPU-bound tasks.
When we say non-blocking IO, we mean that execution of the program does not wait for an IO operation (e.g., filesystem read/write, database call, network request, ...) to complete before proceeding:
// Bad (blocking)
let data = fs.readFileSync(filePath)
console.log(data) // Execute BEFORE somethingUnrelated()
somethingUnrelated()
// Good (non-blocking)
fs.promise.readFile(filePath)
.then(console.log) // Execute AFTER somethingUnrelated()
somethingUnrelated()
The primary benefit and virtue of non-blocking IO and an event-loop based runtime is cooperative multi-tasking (allowing for reduced latency for IO-bound tasks) and a much lower memory footprint due to the elimination of thread overhead.
First, setup your environment.
Second, you have 3 options for writing your node.js Hello World:
$ node
> console.log('hello world!')
hello world
undefined
Note: undefined
is the return value of the previous statement.
-e
$ node -e "console.log('hello world!')"
hello world
// index.js
console.log('hello world!')
$ node index.js
hello world
npm
The Node Package Manager (npm
), comes with node.js. npm
provides the following functionality:
Install/publish 3rd-party packages & dependencies
npm install lodash --save # --save adds the dependency to the package.json
Utility operations for running applications (e.g., npm init
, npm start
, npm test
& npm shrinkwrap
)
To learn more, see the Modules & Packages Guide as well as the highly recommended self-paced how-to-npm
Nodeschool.io Workshopper.
Without covering the specifics of the various core modules or rehashing topics like Filesystem, Control-flow, Modules & Packages, Networking or JavaScript covered already in other guides, there's not much else to cover.
Nodeschool.io is a set of self-paced community built and maintained workshops ("Workshoppers") for learning and building solutions to various common core concepts. It is highly recommended that you run through the following workshoppers:
javascripting
- JavaScriptlearnyounode
- node.js fundamentalshow-to-npm
- npm
featuresstream-adventure
- Streams
(Excellent)functional-javascript-workshop
- Functional programming is crucial to writing good code in node.jspromise-it-wont-hurt
- The ES6 Promise
APIcount-to-6
- Other useful ES6 language featuresplanetproto
- Prototypal Inheritancelearn-generators
- Async generators are an ES6 hack for writing ES7 async
functions (will teach async/await
but with function*/yield
instead)Consider completing at least one of the following: