An Absolute Beginner's Guide To Using NPM - NodeSource
An Absolute Beginner's Guide To Using NPM - NodeSource
〉
The NodeSource Blog Search
N E XT P O ST
You have reached the beginning of time!
Choosing the Right Node.js
〉
Package Manager in 2024: A
Comparative Guide
A L L P O STS / H O W TO F E AT U R E D
ARTICLES
An Absolute Beginner's
Node.js Package
Manager in 2024: A
Comparative Guide
SHARE
C AT E G O R I E S
Community
How To
Node.js
Using npm effectively is a cornerstone of modern web development,
NodeSource
no matter if it's exclusively with Node.js, as a package manager or
Product
build tool for the front-end, or even as a piece of workflows in other
languages and on other platforms.
{
"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
}
{
"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).
R E A D N O W : T H E U LT I M AT E G U I D E TO N P M 〉
The project's test command (to trigger testing with something like
Standard)
The project's git repository (where the project source can be found)
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.
H O W TO U S E NPM INIT :
If 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.
”
USAGE:
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
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 .
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 .
USAGE:
USAGE:
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.
USAGE:
STA R T F O R F R E E
© 2024 NodeSource
W H AT W E D O S O LU T I O N S LEARN CO M PA N Y