...
/Tip 48: Leverage Community Knowledge with npm
Tip 48: Leverage Community Knowledge with npm
In this tip, you’ll learn how to import external code with npm.
We'll cover the following...
In the previous tip, you learned how to use code from different files. In this tip, you’ll learn how to use community code created by developers all over the world.
Access external code using npm
Not many years ago, if you wanted to use an open-source library, you were forced to either copy-paste code locally, download a library to your project, or include an external dependency using a <script>
tag in your markup.
You’d get your code—unless an external source went down—but it was hard to keep dependencies up to date, particularly if you were storing them locally. Not only was it hard to maintain, but you also had to write your custom code assuming the library would be there. This made code really hard to read and test because you never explicitly included anything.
Those days are gone. You can now download code directly to your project, control versions, and import code into individual files using familiar conventions.
You manage all this with a tool called the node package manager, or npm. There are a few alternatives, such as Facebook’s yarn project, but they work in mostly the same way, so don’t worry too much about the differences.
npm is an important project, and you’ll mostly use it for importing code, but it can do a lot more. With npm, you can set your project’s metadata and configuration information, run command-line scripts, and even publish your project for others to use.
Set up
The setup instructions given below have already been done for you on the platform.
To run the code locally, you’ll need to have ...