We previously saw how to import functions exported from other files.
But we still had to write those functions. What if other people wrote code and published it, and we can just import it? In this way, we could be much more productive, and share great code with all the other JavaScript developers!
Apart from the couple dozens of built-in modules, we can use 3rd party modules.
Enter Node.js modules, also called npm packages.
Running a single command, using npm
, we can ask Node.js to fetch packages other developers published.
Here’s how it works.
People work on something, they package it in a specific format and upload it on GitHub, a code hosting platform.
Then they set up the package on https://www.npmjs.com, the site responsible for hosting Node.js packages.
In any project, you can now use that package through the Node.js utility called npm
.
Packages can do very trivial things, or they can do very complex stuff.
For example my-prime
https://github.com/jinnatul/my-prime
Its unique value proposition is to give you a function that tells you true
if a number is prime.
Very simple.
But perfect for our first 3rd party module usage.
Install it using
npm install my-prime
NOTE: if you can’t find the
package.json
andnode_modules
folder after running thenpm install <package>
command, it’s likely because you have a parent folder that contains those, like your home folder, and this confuses npm. To fix this problem, runnpm init -y
in the folder to create a blankpackage.json
file, then re-run thenpm install <package>
command.
Also, if you notice you have for example package.json
and node_modules
in your home folder, delete those. You probably ran npm install <package>
in your home folder by mistake, and they are not useful except to create this ☝️ problem.
Note that this action creates 3 things: a node_modules
folder with the package in its folder:
A package.json
file with the dependency:
And a package-lock.json
that contains metadata.
Now you can require this module into a program.
Create an app.js
file for example:
const pr = require('my-prime')
console.log(pr.isPrime(2))
console.log(pr.isPrime(4))
Then run it with node app.js
.
This program will print true
on line 3, and false
on line 4.
⚠️ NOTE if you get an error like this:
ReferenceError: require is not defined i
n ES module scope, you can use import instead
This file is being treated as an ES module
because it has a '.js' file extension
make sure you don’t have the line "type": "module",
in your package.json
file as that enables ES modules syntax instead of require()
This was your first Node module usage.
When writing our programs we’ll rely on using npm install <package>
all the time.
Instead of reinventing the wheel all the time, we can use code written for us by people and organizations, to make our life easier.
You can run npm uninstall my-prime
in the shell now to remove the package.
npm
is super cool.
You typically store projects on GitHub without the node_modules
folder, which can grow considerably in size.
So you might download a project that has a packages.json
file, but no node_modules
folder.
You run:
npm install
and npm
will install everything the project needs in the node_modules
folder, creating it if it’s not existing already.
Updating is also made easy, by running
npm update
npm
will check all packages for a newer version.
You can specify a single package to update as well:
npm update <package-name>
In addition to downloads, npm
also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.
Many times you’ll find that a library is only compatible with a major release of another library.
Or a bug in the latest release of a lib, still unfixed, is causing an issue.
Specifying an explicit version of a library also helps to keep everyone on the same exact version of a package, so that the whole team runs the same version until the package.json
file is updated.
In all those cases, versioning helps a lot, and npm
follows the semantic versioning (“semver”) standard.
The package.json
file also supports a format for specifying command-line tasks that can be run by using the npm run ...
syntax.
For example, many tools use the npm run dev
to offer a quick way to get the development version up and running locally, and npm run build
to create the production build.
We’ll use this all the time to run our projects.
npx
Things don’t end here - npx
is another command that’s installed with Node.js on your computer.
npx
lets you run code built with Node and published through the npm registry.
Basically maintainers can publish projects that contain executables, and we use those to perform specific tasks.
We’ll use in other units for example to create a Remix project. Or add an integration to Astro. Or initialize Tailwind CSS.
I won’t go more into it here, but now you know what it is.
Lessons in this unit:
0: | Introduction |
1: | Installing Node.js on your computer |
2: | How to write your first Node.js program |
3: | Importing other files |
4: | ▶︎ Using npm to install packages |
5: | Using built-in modules |