What is NPM?
NPM hosts thousands of free packages to download and use.
The NPM program is installed on your computer when you install Node.js
What is a Package?
A package in Node.js contains all the files you need for a module.
Modules are JavaScript libraries you can include in your project.
You can add a package.json file to your package to make it easy for others to manage and install. Packages published to the registry must contain a package.json file.
Run the following command on the command line:
npm init
npm init --yes
npm install
// alias
npm i
This command installs a package, and any packages that it depends on. If the package has a package-lock or shrinkwrap file, the installation of dependencies will be driven by that, with an npm-shrinkwrap.json taking precedence if both files exist. See package-lock.json and npm-shrinkwrap.
npm install --save-dev
// or
npm install -D
The difference between these two, is that devDependencies are modules which are only required during development, while dependencies are modules which are also required at runtime.
global mode
npm install --global
// alias
npm i -g
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
Once you have installed a package in node_modules, you can use it in your code.
If you are creating a Node.js module, you can use a package in your module by passing it as an argument to the require function.
var lodash = require('lodash');
const yourModule = require('/path')
To get your own module use require + module path:
To export your own module use module.exports:
const myFunction = () => { /* Function body */ };
module.exports = myFunction;
// or
module.exports = {
myFunction
}
Do you want some street magic?
How to run my web app using Node.js modules in browser?
With Browserify you can write code that uses require in the same way that you would use it in Node.
Want to use ES6 import and export ?
npm
By Daniel Suleiman
npm
- 675