In this volume of Need to Node, you can find the latest news on Node.js’s v13.12.0 (Current) Release, Diagnostics in Node.js and a practical guide to memory leaks in Node.js.
Need to Node is a weekly bulletin designed to keep you up-to-date with the latest news on the Node.js project, events and articles. You are always welcome to collaborate and participate. Please let us know if we missed a piece of content you think should be included!
If you find any Node.js or JavaScript related content over the next week (or beyond!), never hesitate to reach out to us on Twitter at@NodeSource to share and get it included in Need to Node - our DMs are open if you don’t want to share publicly!
Read MoreThis article was first published in NodeSource blog on February 2017.
Using npm effectively is a cornerstone of modern web development, no matter if it's exclusively with Node.js, as a package manager or build tool for the front-end, or even as a piece of workflows in other languages and on other platforms.
Really understanding npm as a tool, understanding the core concepts, can be something that's difficult for a beginner - I spent many hours just trying to figure out small details that would seem minor or be taken for granted by others.
As such, I've written up a basic and detailed guide for understanding npm, for those who are entirely new to Node.js, npm, and the surrounding ecosystem.
package.json
As a general rule, any project that's using Node.js will need to have a package.json
file. What is a package.json
file?
At its simplest, a package.json
file can be described as a manifest of your project that includes the packages and applications it depends on, information about its unique source control, and specific metadata like the project's name, description, and author.
Let's break down the core parts of a typical package.json
file:
Inside a package.json, you'll almost always find metadata specific to the project - no matter if it's a web application, Node.js module, or even just a plain JavaScirpt library. This metadata helps identify the project and acts as a baseline for users and contributors to get information about the project.
Here's an example of how these fields would look in a package.json file:
{
"name": "metaverse", // The name of your project
"version": "0.92.12", // The version of your project
"description": "The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.", // The description of your project
"main": "index.js"
"license": "MIT" // The license of your project
}
A package.json
file is always structured in the JSON format, which allows it to be easily read as metadata and parsed by machines.
If needing to format a package.json
file manually to get your project up and running seems a bit daunting, there's a handy command that will automatically generate a base package.json
file for you - if you'd like to learn how to use it, take a peek at the npm init
instructions below!
dependencies
and devDepenendcies
in your package.json
The other majorly important aspect of a package.json
is that it contains a collection of any given project's dependencies. These dependencies are the modules that the project relies on to function properly.
Having dependencies in your project's package.json
allows the project to install the versions of the modules it depends on. By running an install command (see the instructions for npm install
below) inside of a project, you can install all of the dependencies that are listed in the project's package.json
- meaning they don't have to be (and almost never should be) bundled with the project itself.
Second, it allows the separation of dependencies that are needed for production and dependencies that are needed for development. In production, you're likely not going to need a tool to watch your CSS files for changes and refresh the app when they change. But in both production and development, you'll want to have the modules that enable what you're trying to accomplish with your project - things like your web framework, API tools, and code utilities.
What would a project's package.json
look like with dependencies
and devDependencies
? Let's expand on the previous example of a package.json
to include some.
{
"name": "metaverse",
"version": "0.92.12",
"description": "The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.",
"main": "index.js"
"license": "MIT",
"devDependencies": {
"mocha": "~3.1",
"native-hello-world": "^1.0.0",
"should": "~3.3",
"sinon": "~1.9"
},
"dependencies": {
"fill-keys": "^1.0.2",
"module-not-found-error": "^1.0.0",
"resolve": "~1.1.7"
}
}
One key difference between the dependencies and the other common parts of a package.json
is that they're both objects, with multiple key/value pairs. Every key in both dependencies
and devDependencies
is a name of a package, and every value is the version range that's acceptable to install (according to Semantic Versioning - to learn more about Semantic Versioning, also known as semver, check out our primer on semver).
When using npm, you're most likely going to be using the command line tool for the majority of your interactions. As such, here's a detailed rundown of the commands that you'll encounter and need to use most frequently.
npm init
to Initialize a ProjectThe npm init
command is a step-by-step tool to scaffold out your project. It will prompt you for input for a few aspects of the project in the following order:
It's worth noting that if you're content with the suggestion that the npm init
command provides next to the prompt, you can simply hit Return
or Enter
to accept the suggestion and move on to the next prompt.
Once you run through the npm init
steps above, a package.json
file will be generated and placed in the current directory. If you run it in a directory that's not exclusively for your project, don't worry! Generating a package.json
doesn't really do anything, other than create a package.json
file. You can either move the package.json
file to a directory that's dedicated to your project, or you can create an entirely new one in such a directory.
npm init
:npm init # This will trigger the initialization
npm init --yes
to Instantly Initialize a ProjectIf you want to get on to building your project, and don't want to spend the (albeit brief) time answering the prompts that come from npm init
, you can use the --yes
flag on the npm init
command to automatically populate all options with the default npm init
values.
Note: You can configure what these default values are with the npm configuration - that's a more advanced topic, and outside the scope of this beginner's guide to npm.
That said, if you're interested in setting that up, you can learn how to set these defaults in the eleventh tip of our npm tricks article.
npm init --yes # This will trigger automatically populated initialization.
npm install
Installing modules from npm is one of the most basic things you should learn to do when getting started with npm. As you dive deeper, you'll begin to learn some variations on installing modules, but here's the very core of what you need to know to install a standalone module into the current directory:
npm install <module>
In the above command, you'd replace <module>
with the name of the module you want to install. For example, if you want to install Express (the most used and most well known Node.js web framework), you could run the following command:
npm install express
The above command will install the express
module into /node_modules
in the current directory. Whenever you install a module from npm, it will be installed into the node_modules
folder.
In addition to triggering an install of a single module, you can actually trigger the installation of all modules that are listed as dependencies
and devDependencies
in the package.json
in the current directory. To do so, you'll simply need to run the command itself:
npm install
Once you run this, npm will begin the installation process of all of the current project's dependencies.
As an aside, one thing to note is that there's an alias for npm install
that you may see in the wild when working with modules from the ecosystem. The alias is npm i
, where i
takes the place of install
.
This seemingly minor alias is a small gotcha for beginners - including myself, several times when I was learning - to the Node.js and npm ecosystems, as there's not a standardized, single way that module creators and maintainers will instruct on how to install their module.
npm install <module> # Where <module> is the name of the module you want to install
npm i <module> # Where <module> is the name of the module you want to install - using the i alias for installation
package.json
as a dependencyAs with npm init
, the npm install
command has a flag or two that you'll find useful in your workflow - it'll save you time and effort with regard to your project's package.json
file.
When you're running npm install
to install a module, you can add the optional flag --save
to the command. This flag will add the module as a dependency of your project to the project's package.json
as an entry in dependencies
.
npm install <module> --save # Where <module> is the name of the module you want to install
package.json
as a Developer dependencyThere's a flag that is nearly an exact duplicate, in terms of functionality, of the --save
flag when installing a module: --save-dev
. There are a few a key differences between the two - instead of saving the module being installed and added to package.json
as an entry in dependencies
, it will save it as an entry in the devDependencies
.
The semantic difference here is that dependencies
are for use in production - whatever that would entail for your project. On the other hand, devDependencies
are a collection of the dependencies that are used in development of your application - the modules that you use to build it, but don't need to use when it's running. This could include things like testing tools, a local server to speed up your development, and more.
npm install <module> --save-dev # Where <module> is the name of the module you want to install
The final, and most common, flag for npm install
that you should are the flags to install a module globally on your system.
Global modules can be extremely useful - there are tons tools, utilities, and more for both development and general usage that you can install globally to use.
To install a module from npm globally, you'll simply need to use the --global
flag when running the install command to have the module install globally, rather than locally (to the current directory).
Note: One caveat with global modules is that, by default, npm will install them to a system directory, not a local one. With this as the default, you'll need to authenticate as a privileged user on your system to install global modules.
As a best practice, you should change the default installation location from a system directory to a user directory. If you'd like to learn to do this, take a peek at the seventh tip in our npm tricks article!
npm install <module> --global # Where <module> is the name of the module you want to install globally
npm install <module> -g # Where <module> is the name of the module you want to install globally, using the -g alias
If you want to keep learning about npm and all its facets, I've got a few awesome things for you. A bit ago, we shared a few npm tricks to knock your wombat socks off. Even better, we wrote a follow-up with even more npm tricks! This beginner's guide is a great springboard to get off the ground, and both of those will help you start optimizing your work with npm! If you'd like to go even further with npm and start deploying Node.js apps and npm modules into production, you should definitely take a look at NodeSource Certified Modules - it's an awesome tool that'll compliment your newly acquired npm skills!
Read MoreThis article was first published in NodeSource blog on February 2017.
As with any programming language, platform, or tool that doesn't come bundled with Windows, getting up and running with Node.js takes some initial setup before you can start hacking away. In my experience, though Node.js has a far better installation experience on Windows than virtually any other language, platform, or tool that I've tried to use - just run the installer, and you're good to go.
In this quick tutorial, we'll take a look at how to get Node.js installed on Windows. Once we've completed the entirety of the tutorial, you'll be ready to take the next step with Node.js.
This guide covers installing Node.js on the following versions of Windows: Windows 7, Windows 8, Windows 8.1, and Windows 10. These are the versions that are consistently tested and supported by the Node.js build process at the time of writing.
Here's the abbreviated guide, highlighting the major steps:
Run the downloaded Node.js .msi
Installer - including accepting the license, selecting the destination, and authenticating for the install.
node -v
in your terminal - you should get something like v6.9.5
npm install npm --global
.msi
InstallerAs the first step to installing Node.js on Windows, you'll need to download the installer. You'll be able to grab the installer from the official downloads page for Node.js.
You'll be able to download the Windows Node.js installer by clicking the Windows Installer
option at the top of the page - when you click this, you'll get an MSI installer download. Make sure to save it somewhere that you'll be able to find it!
You've got the Windows Installer - great! Now, you need to install it on your PC. The installer is a pretty typical Wizard interface for installing software on Windows - there are a few steps to it, but you can have it done in under a minute. You can get through it by following the guide below:
Welcome to the Node.js Setup Wizard
Next
End-User License Agreement (EULA)
I accept the terms in the License Agreement
Next
Destination Folder
Next
Custom Setup
Next
Ready to install Node.js
Install
Installing Node.js
Completed the Node.js Setup Wizard
Finish
To double check that Node.js was installed fully on your PC, you can test the following command in your Command Prompt (regardless of if you're using cmd.exe
, Powershell
, or any other command prompt):
$ node -v
If Node.js was installed fully, the command prompt will print something similar to (but probably not exactly) this:
$ node -v // The command we ran - prints out the version of Node.js that's currently installed
v6.9.5 // The printed version of Node.js that's currently installed - v6.9.5 was the most current LTS release at the time of writing.
As the final step in getting Node.js installed, we'll update your version of npm - the package manager that comes bundled with Node.js.
Node.js always ships with a specific version of npm - Node.js doesn't (and shouldn't!) automatically update npm. The release cycle of the npm CLI client isn't in sync with the Node.js releases. Because of this, there's almost certainly going to be a newer version of npm available than the one that is installed as a default in any given Node release.
To quickly and easily update npm
, you can run the following command:
npm install npm --global // Update the `npm` CLI client
Now you've got Node.js on Windows machine. It's time to start exploring!
Luckily, we've got your back. We've written a bunch of articles to help you get started with Node.js! If you're interested in exploring ES6, you should check out our article on some of the most exciting ES6 features in Node.js. If you're interested in ways to collaborate with your team a bit better, you should check out our tutorial on using ESLint in projects to improve team collaboration through code standards. Maybe you'd just like to start deploying the applications you're about to build? In that case, take a peek at our guide on how to deploy Node.js apps on Linux with systemd!
That said, if you want to keep in touch with Node.js and the surrounding ecosystem, you should go follow @NodeSource on Twitter! We'll keep you updated with important news from the Node.js project, and share the best Node.js tutorials, guides, and tools that the community has to offer.
Read MoreJust like any programming language, platform, or library, getting up and running with Node.js takes some initial setup before you can start hacking away. With Node.js, the only initial setup required is, quite simply, getting the binary installed.
In this quick tutorial, we'll take a quick look at how to get Node.js on macOS. Once we've completed the entirety of the tutorial, you'll be ready to take the next step with Node.js.
This guide covers installing Node.js on the following versions of OS X and macOS: OS X 10.10 (Yosemite), OS X 10.11 (El Capitan), and macOS 10.11. These are the versions that are consistently tested and supported by the Node.js build process at the time of writing.
Here's the abbreviated guide, highlighting the major steps:
.pkg
Installernode -v
in your terminal - you should get something like v6.9.4
.pkg
InstallerAs our first step, we need to actually get the official installer for Node.js on macOS. To do so, we can head over to the Node.js Downloads page to download the installer.
You can get the macOS installer by clicking the Macintosh Installer
option - this will download the .pkg
installer for Node.js. Make sure you save it somewhere that you'll be able to access it!
Now that you've got the installer downloaded, you'll need to run it. The installer is a pretty typical interface - it won't take long to get through it (under a minute), even though there are a few parts to it. You can get through it by following the guide below:
Introduction
Continue
License
Continue
Agree
Installation Type
Install
Install Software
Summary
Close
To verify that Node.js was installed correctly on your Mac, you can run the following command in your terminal:
$ node -v
If Node.js was properly installed, you'll see something close to (but probably not exactly) this:
$ node -v // The command we ran - tests the version of Node.js that's currently installed
v6.9.4 // The version of Node.js that's installed - v6.9.4 was the most current LTS release at the time of writing.
As one last step for good measure, we'll update your version of npm.
Node.js always ships with a specific version of npm - Node.js doesn't (and shouldn't!) automatically update npm. The npm releases aren't synced with Node.js releases. Because of this, there's almost always a newer version of npm than the one that is installed by default with a given version of Node.
To easily update your version of npm
, you can run the following command:
$ sudo npm install npm --global // Update the `npm` CLI client
Now you've got Node.js on your Mac. It's time to start exploring!
Thankfully, we've got your back. We've got a ton of articles on getting started with Node.js! If you're interested in exploring ES6, you should check out our article on some of the most exciting ES6 features in Node.js. Looking for ways to standardize your JavaScript code across your team? In that case, you should check out our guide to using ESLint to build code standards in Node.js applications. Maybe you'd just like to start deploying your applications? In that case, check out our guide on deploying Node.js apps with systemd!
That said, if you want to keep in touch with Node.js and the surrounding ecosystem, you should go follow @NodeSource on Twitter! We'll keep you updated with important news from the Node.js project, and share the best Node.js tutorials, guides, and tools that the community has to offer!
Read MoreIn this volume of Need to Node, you can find the latest news on the Node.js v13.11.0 (Current) Release, npm is joining GitHub and Three Things You Didn't Know You Could Do with npm Scripts.
Need to Node is a weekly bulletin designed to keep you up to date with the latest news on the Node.js project, events and awesome articles. You are always welcome to collaborate and participate. Please let us know if we missed a piece of content you think should be included!
async_hooks
, cli
and fs
.^
, >=
, or *
required. Exciting! By Valeri Karpov If you find any Node.js or JavaScript related content over the next week (or beyond!), never hesitate to reach out to us on Twitter at @NodeSource to share and get it included in Need to Node - our DMs are open if you don’t want to share publicly!
Read MoreThis article was first published in NodeSource blog on January 2017.
As with any programming language, platform, or tool, the first step to using it is getting it installed. Many of them typically come with a speedy way to upgrade when a new version is available.
By default, there's not a way to upgrade the version of Node.js you've got from within Node.js itself. That said, there's a fantastic tool for the community called nvm that allows you to manage the versions of Node.js that you've got installed locally.
One awesome aspect of nvm
is that it manages the versions of Node.js, it doesn't just upgrade them. This means you can have the latest version of Node.js, the latest versions of all the LTS release lines, and any number of other versions you want to use or test as well.
In this quick tutorial, we'll take a look at how to install nvm, and then how to start using it as your version manager for Node.js. Once we've completed the tutorial, you'll be ready to take the next step with Node.js.
This guide covers installing nvm on macOS and Linux - note that all versions of Node.js may not support every version of macOS or Linux.
Here's the abbreviated guide, highlighting the major steps:
Download the nvm install script via cURL:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
nvm --version
, which should return the version of nvm installed.Install the version of Node.js you want
nvm install node
nvm use node
nvm install --lts
nvm use --lts
In some cases, like when installing Node.js releases from their source or installing versions of Node.js before 0.8.6
(when the project started shipping binaries), you'll need to ensure that your system has the appropriate C++ build tools.
For LTS and modern releases, you will not need this step. That said, it's a nice to have to ensure that the majority of requirements are met in any scenario.
On macOS, you've got two options for a C++ compiler: the full XCode application or the stand-alone Command Line Tools portion of Xcode.
To get these on macOS, you can follow these steps:
Run xcode-select --install
as a command
Install
On Linux, the C++ compiler will vary from distribution to distribution. For example, on Debian and Ubuntu, you'll need to install build-tools
and libssl-dev
, but this may be different on your given Linux distribution.
To get build-tools
and libssl-dev
on Debuan and Ubuntu distributions, you can run these commands:
sudo apt-get install build-essential # Install the build-essential package - let this run to completion
sudo apt-get install libssl-dev # Install the libssl-dev package - also let this one run to completion
Once you've got the right C++ compiler for your system, now it's time to run the nvm install script. Here are the single-step install scripts for both macOS and Linux. You've got the option of cURL or Wget but both achieve the same result.
Note: If your Linux system doesn't have either cURL or Wget, you can run sudo apt-get install curl
and use the cURL method.
To install nvm with the cURL method, run the following command in your terminal:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
To install nvm with the Wget method, run the following command in your terminal:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
After running the install script from Step 2, nvm should have successfully installed. To ensure that nvm is up and running on your machine, you can test it with the following command:
nvm --version
This command will return something like (though not necessarily exactly) the following:
nvm --version # The command we ran - it checks the currently installed version of nvm
0.33.0 # The current version of nvm - yours may differ!
nvm
command after running the install script?If you're using macOS, you may be missing a .bash_profile
file - to troubleshoot this, you can run touch ~/.bash_profile
in your command line and re-run the installer script.
If the problem persists after that, you can open the existing .bash_profile
file (using your favorite text editor) and add the following line to it:
source ~/.bashrc
If you're still having issues, you can take a peek at this issue to find a discussion of the problem and a collection of possible resolutions.
Congratulations! You've now got nvm
- a tool to easily allow you to manage and swap out the versions of Node.js you've got installed locally. Now, let's get you started with doing just that.
To install the latest available version of Node.js, you can use the following command:
nvm install node
Next, to use that version of Node.js in any new shell, you can simply run the use
command:
nvm use node
To install the latest available LTS version of Node.js, you can run the following command:
nvm install --lts
And to use that latatestTS version of Node.js in any new shell, you can simply run the use
command:
nvm use --lts
Now you've got a fantastic version manager for Node.js. It's time to start building!
We've got some resources to get you kickstarted! Both the breadth and depth of the Node.js and the JavaScript ecosystems are quite large - in addition to the developer tools like NodeSource N|Solid and Certified Modules, we've got a ton of tutorials, guides, and articles to help you get kick started with Node.js.
If you're interested in keeping your code clean, maintainable, and collaborative, take a peek at our post on using ESLint for linting your JavaScript applications. Are you interested in building web applications with Node.js? One of the most challenging aspects of web apps is security - you can learn security best practices for Express to lock down your web apps, to prevent breaches and attacks. Or, maybe you want to deploy your Node.js apps with Docker? Then you should definitely read our article on dockerizing your Node.js applications.
That said, if you want to keep in touch with the Node.js ecosystem, you should follow @NodeSource on Twitter! We'll keep you updated with important news from the core Node.js project, fresh and useful Node.js tutorials, and more.
Read More