Mastering_package.json[1]
Mastering_package.json[1]
package.json
by
Mohammad M Khani
What is package.json?
In
In Node.js,
Node.js, package.json
package.json is is aa JSON
JSON file
file that
that stores
stores
important
important information
information about
about youryour project.
project. It
It acts
acts as
as aa
map
map for
for Node
Node and
and npmnpm (Node
(Node Package
Package Manager),
Manager),
telling
telling them
them what
what your
your project
project is,is, what
what dependencies
dependencies
it
it requires,
requires, how
how it it should
should be
be run,
run, and
and much
much more.
more.
Essentially,
Essentially, it’s
it’s the
the go-to
go-to reference
reference for for any
any Node
Node
application
application or or library.
library.
Imagine
Imagine package.json
package.json as as the
the IDID card
card for
for your
your project:
project:
it
it identifies
identifies the
the project
project name,
name, version,
version, description,
description,
and
and everything
everything required
required to
to install,
install, run,
run, and
and maintain
maintain
the
the project.
project.
Core Fields in package.json
11.. name:
name:
This
This is
is the
the name
name ofof your
your package.
package. ItIt must
must be
be
lowercase
lowercase andand URL-safe
URL-safe
22.. version:
version:
Follows
Follows Semantic
Semantic Versioning
Versioning (MAJOR.MINOR.PATCH).
(MAJOR.MINOR.PATCH).
33.. description:
description:
AA brief
brief explanation
explanation of of what
what your
your package
package does.
does.
44.. main:
main:
Specifies
Specifies the
the entry
entry point
point of
of your
your package
package (e.g.,
(e.g., the
the
main
main JavaScript
JavaScript file).
file).
55.. scripts:
scripts:
Allows
Allows you
you to
to define
define custom
custom commands
commands that
that can
can be
be
run
run via
via npm
npm run.
run.
66.. keywords:
keywords:
An
An array
array of
of words
words or or phrases
phrases to
to make
make your
your package
package
more
more discoverable.
discoverable.
77.. author:
author:
Information
Information about
about the
the package
package author.
author.
88.. license:
license:
Specifies
Specifies the
the license
license under
under which
which the
the package
package isis
distributed.
distributed.
Core Fields in package.json
examples :
Dependencies in package.json
11.. dependencies:
dependencies:
Lists
Lists packages
packages required
required toto
run
run your
your application.
application.
22.. devDependencies:
devDependencies:
Lists
Lists packages
packages needed
needed only
only
for
for development.
development.
33.. peerDependencies:
peerDependencies:
Used
Used toto specify
specify packages
packages
that
that your
your package
package depends
depends
on
on but
but doesn't
doesn't install
install itself.
itself.
44.. optionalDependencies:
optionalDependencies:
Packages
Packages that
that are
are optional
optional
for
for your
your application.
application.
Versioning in package.json
The
The version
version field
field in
in package.json
package.json uses
uses Semantic
Semantic
Versioning
Versioning (or (or SemVer),
SemVer), which
which defines
defines version
version numbers
numbers inin
aa major.minor.patch
major.minor.patch format.format.
Major:
Major: Incremented
Incremented for for incompatible
incompatible APIAPI changes.
changes.
Minor:
Minor: Incremented
Incremented for for backward-compatible
backward-compatible new new
features.
features.
Patch:
Patch: Incremented
Incremented for for backward-compatible
backward-compatible bug bug
fixes.
fixes.
For
For example,
example, changing
changing 1.0.0
1.0.0 to
to 1.1.0
1.1.0 would
would indicate
indicate aa
minor
minor update
update withwith new
new features,
features, while
while 2.0.0
2.0.0 would
would imply
imply
breaking
breaking changes.
changes.
When
When managing
managing dependencies,
dependencies, you you might
might see
see version
version
symbols
symbols like
like ^^ and
and ~:
~:
^:
^: Allows
Allows updates
updates forfor the
the latest
latest minor
minor or
or patch
patch
version.
version.
~:
~: Only
Only allows
allows updates
updates forfor the
the latest
latest patch
patch version.
version.
These
These symbols
symbols helphelp keep
keep dependencies
dependencies updated
updated without
without
introducing
introducing unexpected
unexpected breaking
breaking changes.
changes.
Advanced Fields
11.. engines:
engines:
SSpecifies
pecifies the
the Node.js
Node.js versions
versions your
your package
package supports.
supports.
22.. browserslist:
browserslist:
Specifies
Specifies browser
browser compatibility
compatibility for
for your
your project.
project.
33.. type:
type:
Specifies
Specifies whether
whether the
the package
package uses
uses CommonJS
CommonJS or
or ES
ES
Modules.
Modules.
Best Practices for
package.json
11.. Use
Use Semantic
Semantic Versioning
Versioning for
for your
your version
version field
field to
to
ensure
ensure compatibility.
compatibility.
22.. Keep
Keep dependencies
dependencies updated
updated but
but be
be mindful
mindful of
of breaking
breaking
changes.
changes.
33.. Use
Use meaningful
meaningful scripts
scripts to
to automate
automate workflows
workflows (e.g.,
(e.g.,
testing,
testing, linting,
linting, building).
building).
44.. Document
Document fields
fields like
like author
author and
and description
description for
for better
better
readability.
readability.
55.. Use
Use browserslist
browserslist toto ensure
ensure your
your app
app works
works for
for target
target
browsers.
browsers.
DON'T FORGET
TO Like , Share
and repost
AND IF
YOU LIKE THIS
POST
by
Mohammad M Khani