// Node.js uses the CommonJS module system, while in the browser we are
starting to see the ES Modules standard being implemented.
V8 JavaScript Engine
| Browsers | JS engines |
|---|---|
| Google Chrome | V8 |
| Mozilla Firefox | SpiderMonkey |
| Safari | JavaScriptCore (also called Nitro) |
| Edge | V8, earlier built on Chakra |
All these engines implement the ECMA ES-262 standard, also called ECMAScript, the standard used by JavaScript.
Node uses v8, which is written in C++
// JavaScript is generally considered an interpreted language, but
modern JavaScript engines no longer just interpret JavaScript, they
compile it.
Using Command Line
Running JS file
Run the main Node JS application file using the command
node {file_name.js}
REPL mode
Alternatively omit the filename, to use Node JS in REPL (Read Evaluate Print Loop) mode
REPL is a programming language environment (basically a console window) that takes single expression as user input and returns the result back to the console after execution.
- className + <tab> will show all the available methods of that particular class in REPL mode.
- global + <tab> will show all the global objects.
- '_' prints the result of the last operation.
- REPL has some special commands (starting with '.'): .help, .editor, .break, .clear, .load, .save, .exit
Command line args
- Pass command line args after the file name, and retrieve them from process.argv or use minimist library.
- use readline module and readline-sync package to read input from the command line. A more complete and abstract solution is provided by the Inquirer package.
Command line output
- use console.log() and console.err() to print, console.clear() to clear
- console.count() and console.counterReset() to count and reset number of times a particular string is print.
- console.trace() to print the error trace.
- use time() and timeEnd() to calculate time
- use escape sequences to color the output of the text. Alternatively, use chalk library.
- create a progress bar using progress library.
NPM Package Manager
install / update
- npm install [package_name [@ version]] to install npm packages.
- npm update [package_name] to install npm packages.
// [package_name] is optional. If not provided, all the packages in
package.json file are installed / updated
// a particular version can be installed by adding the version in the command
// local npm packages are stored in the node_modules folder
// global npm packages are stored in a pre-defined path, which can be found using npm root -g command
// a particular version can be installed by adding the version in the command
// local npm packages are stored in the node_modules folder
// global npm packages are stored in a pre-defined path, which can be found using npm root -g command
Following flags can be used
- --save-dev installs and adds the entry to the package.json file devDependencies
- --no-save installs but does not add the entry to the package.json file dependencies
- --save-optional installs and adds the entry to the package.json file optionalDependencies
- --no-optional will prevent optional dependencies from being installed
- -g installs the npm package globally
// The difference between devDependencies and dependencies is that the
former contains development tools, like a testing library, while the
latter is bundled with the app in production.
// As for the optionalDependencies the difference is that build failure of the dependency will not cause installation to fail.
// As for the optionalDependencies the difference is that build failure of the dependency will not cause installation to fail.
run task
- npm run {task-name} can be used to run a specific task. Task names are provided in the 'scripts' object in package.json file
uninstall
- npm uninstall {package-name}
Other Important Commands
- npm list lists the version of all installed npm packages, including their dependencies
- npm list [package_name]
- npm view [package_name] version to see the latest available version of the package on the npm repository,
Versioning Symbols
- ^: It will only do updates that do not change the leftmost non-zero number i.e there can be changes in minor version or patch version but not in major version. If you write ^13.1.0, when running npm update, it can update to 13.2.0, 13.3.0 even 13.3.1, 13.3.2 and so on, but not to 14.0.0 or above.
- ~: if you write ~0.13.0 when running npm update it can update to patch releases: 0.13.1 is ok, but 0.14.0 is not.
- >: you accept any version higher than the one you specify
- >=: you accept any version equal to or higher than the one you specify
- <=: you accept any version equal or lower to the one you specify
- <: you accept any version lower than the one you specify
- =: you accept that exact version
- -: you accept a range of versions. Example: 2.1.0 - 2.6.2
- ||: you combine sets. Example: < 2.1 || > 2.6
package.json guide
Useful keys
- name sets the application/package name
- author lists the package author name
- contributors is an array that lists all the contributors.
- bugs links to the package issue tracker, most likely a GitHub issues page.
- homepageSets the package homepage
- version indicates the current version
- license indicates the license of the package
- keywords contains an array of keywords that associate with what your package does.
- description is a brief description of the app/package
- repository specifies where this package repository is located.
- main set the entry point for the application
- private if set to true prevents the app/package to be accidentally published on npm
- scripts defines a set of node scripts you can run
- dependencies sets a list of npm packages installed as dependencies
- devDependencies sets a list of npm packages installed as development dependencies
- engines sets which versions of Node.js this package/app works on
- browserslist is used to tell which browsers (and their versions) you want to support
// The name must be less than 214 characters, must not have spaces, it
can only contain lowercase letters, hyphens (-) or underscores (_).
// This property follows the semantic versioning (semver) notation for versions, which means the version is always expressed with 3 numbers: x.x.x. The first number is the major version, the second the minor version and the third is the patch version.
// This property follows the semantic versioning (semver) notation for versions, which means the version is always expressed with 3 numbers: x.x.x. The first number is the major version, the second the minor version and the third is the patch version.
package-lock.json guide
- The goal of package-lock.json file is to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers.
- The package-lock.json sets your currently installed version of each package in stone, and npm will use those exact versions when running npm install
- The dependencies versions will be updated in the package-lock.json file when you run npm update