<aside> 💡 The internet has thousands of already implemented JavaScript code for tons of use cases. These are named libraries / dependencies.
</aside>
Each JavaScript application has a package.json file where these libraries / dependencies are put. This is done for the sake of sharing the code with others.
Run the following command in your desired directory:
npm init
This will generate a basic package.json file for you. For example:
{
"name": "workshop3",
"version": "1.0.0",
"description": "This is for a showcase of how npm works.",
"main": "index.js",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1"
},
"author": "Damian",
"license": "MIT"
}
In order to install a library, you run the following command:
npm install <library-name>
# For example:
npm install convert
After that, you’ll see the package.json file updated with the new dependency:
{
"name": "workshop3",
"version": "1.0.0",
"description": "This is for a showcase of how npm works.",
"main": "index.js",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1"
},
"author": "Damian",
"license": "MIT",
"dependencies": {
"convert": "^4.13.2"
}
}
Now, if you share the apps code with others, they will install the same version of convert library as it is described in package.json.
In order to install all the dependencies the first time you clone the code, you run:
npm install
This will install all the dependencies from package.json with their respective versions.