--reinstall-packages-from=node
option:package.json
file which contains all project dependecies and metadata. It can also include a scripts
section which predefines command aliases. Here is an example of a package.json
file:test
command alias as follows:package.json
file manually, or add new dependencies via the npm install
command with the --save
flag:--save-dev
flag.This is useful for tools which we won’t need in production – e.g. testing libraries. Such dependencies are defined in thedevDependencies
section of the packages.json
../node_modules
folder and it shouldbe in your .gitignore
. You can reinstall all modules with: npm install
../node_modules
and are shared across all Node environments. In general, you should avoid global packages except for global utilitieslike debuggers, compilers, and performance monitoring tools. The -g
flag denotesthat a package is installed globally:node
command.This opens up a REPL for running Node commands interactively. To quit we need to type .exit
:debug
parameter, which starts the script in debug mode. Then you can step through thecode via the continue (c
), step over/next(n
), and step into (s
)commands. The repl
command allows you to check the values ofvariables and expressions in the respective context. Breakpointsare put directly in the code as debugger
statements:nodemon
tool which automatically reloads the Node environment. First you need toinstall it globally:node
command:import
andexport
functionalities. To use them, you need to set-up Babel compilation. Furthermore, most existing JavaScript code usespre-ES6 syntax. Thus, we will overview both ways of working with modules.module.exports
. Client modules would use the require
function to load its value:./friends.js
:friends.js
and a 3rd party module/package moment
:export
command.A module can optionally have a default export, which is automaticallyimported when you include the module:import
command which has several variations.Assuming the previous example is in a file called importExample.js
, we could work with it as follows:proces.env
object. For example:__dirname
and __filename
which denote the paths to the currently executing script/moduleand its directory. This comes handy when accessing resources from relative folders..babelrc
. Hence, we should specify it there as:src/
sub-folder, we can compile to the build/
folder as follows:build/
as normal Node code by using thenode
or nodemon
commands. Babel can monitor for changes, and compileautomatically with the --watch
option:babel-node
command combines compilation and execution in one step. Given a file srct/test.js
you can compile and run as:nodemon
? Here it is:package.json
.Assuming a file src/test.js
exists, we can use:./node_modules/.bin
). Both approaches arepossible.flow
command to your terminal environment. Then we need to initialiseFlow within the node project directory:.flowconfig
file with Flow settings.//@flow
comment at the beginning of the file. It tells Flow to process the file –otherwise it will ignore it. To analyse the code for type violations, just run flow
fromthe project’s folder. This should report all type errors..babelrc
file must be configured to use it:flow-typed
module:Promise
. It is a wrapper of a higher order functionwhich takes two callback functions as parameters called resolve
and reject
. The functioncalls them when the encapsulated operation succeeds or fails respectively. Thefollowing example shows a promise which succeeds if a random number is less than 0.5:then
method, which is also a higher order function.It takes two parameters – a function which is called “on success” and one for failure.Alternatively, we can provide only the “on success” function, and then chain a call to the catch
method: