NPM Cheatsheet

npm
node
javascript
Published

November 18, 2024

Check npm version

image.png 1. Run npm -v in terminal.

Create package.json

image.png 1. In project folder, run npm init. 2. Answer all questions or leave blank to accept default answers.

To skip all prompts and accept all default values, use npm init --yes in project folder

To set default values in package.json

image.png 1. In project folder, run npm config set init-{key we want to change} "{new value}" to set the new value to the config key. For example, if we wanted to change the default author name to the value “MY NAME”: npm config set init-author-name "MY NAME". 2. Confirm that the default value has been set to the new value by running npm init npm init -y. 3. The package.json file will be created in the project folder. image-2.png

Other example: - To change default license type to MIT: npm config set init-license "MIT"

To check the default value for certain key

image.png 1. Run npm get init-author-name

Other example: - To check the default license type: npm get init-license

Remove the default values we’ve set

image.png 1. Run npm config delete {key we want to revert to default before change}.

Some examples: - To revert license to its default: npm config delete init-license. - To revert author to its default: npm config delete init-author-name.

Install node module

image.png 1. In project folder, run npm install {module name} --save. For example, to install lodash module: npm install lodash --save. 2. The module will be downloaded in .node_modules folder in the project directory, and will be included under the dependencies in package.json. image-2.png

Note: - Find node module to install at https://www.npmjs.com/. - --save is important, it writes the module we just installed as the project dependency in package.json. - If we did not include --save, npm will still install the module but will not write it as a depedency.

Import module

image.png 1. To import module we’ve installed const my_new_module = require('library name'). For example, on line 1 above we imported lodash module as _, this is just the convention for lodash. We can name the variable anything we want. 2. We use dot notation to use functions from the imported module. For example on line 4, we call the function each from lodash library by _.each()

To run .js file

image-2.png image.png 1. In the project terminal, run node {name of js file}. Here, the name of the file we want to run is index.js. We could run it with node index or node index.js.

To install all modules listed as dependencies using package.json

image.png 1. Say you fork/clone a repo from github and you want to run their js file locally, you need to first install all the dependencies that their project uses. To do so, first make sure we have package.json file in the project folder. 2. Run npm install. This will install all modules listed under dependencies in package.json. If we have modules listed under devDependencies in package.json, this command will also install of of them. image-4.png image-2.png image-3.png

dev dependencies

dev dependencies are modules that you only need for development and not for production. One example would be gulp module, which is a task runner, used to do things like minify javascript, compile sass file. 1. To install module as dev dependencies, we run npm install {module we want to install} --save-dev. We can also write the --save-dev portion at the front like npm install --save-dev {module we want to install}. image.png In this example, we installed gulp as dev dependencies, and notice that they were listed in devDependencies in package.json file. Also notice that in ./node_modules, we now have all of our project’s dependency + the dependencies of our dependencies. image-3.png

To exclude installing dev dependencies

  1. To install only the modules listed under dependencies, run npm install --production or npm install --omit=dev as the warning stated. image.png image-2.png

To uninstall modules

To uninstall regular dependencies

  1. Use npm uninstall {module name} --save
  2. The module will then be deleted from .node_modules folder. --save flag make sure the module we’ve uninstalled will then be removed from dependencies in package.json.

To uninstall dev dependencies

  1. Use npm uninstall {module name} --save-dev
  2. The module will then be deleted from .node_modules folder. --save flag make sure the module we’ve uninstalled will then be removed from devDependencies in package.json.

Before: image.png image-2.png After: image-3.png

Note: instead of the keyword uninstall, we can also use one of remove rm un.

To install specific version

  1. Run npm install {module name}@{version we want}. For example, if we want to install lodash at version 4.17.3, then we run npm install lodash@4.17.3.

To list all packages you have installed

npm list - This will list everything, including all of the dependencies of your dependencies.

npm list --depth 0 - This will list only your dependencies (and not include your dependencies’ dependencies).

npm list --depth 1 - This will list only your dependencies and their dependencies (and not their dependencies’ dependencies).

To install global modules

  1. Run npm install -g {module we want to install globally}. Eg npm install -g nodemon. Nodemon is a module that can continuously watch your applications so that you don’t have to keep restarting it every time you edited it.

  2. When we install globally, it will be installed on our actual machine, not on the node_modules in the project folder. To check where it is being installed, we run npm root -g.

To remove global modules

  1. npm remove -g {module to remove}. Eg npm remove -g nodemon.

About package.json scripts

image.png We can change the script section of package.json file to make it easy for others to run our code.

For example, we can run our main file directly if we use command npm start. This will look into the package.json file, under the script section and find the keyword start and run the command we saved for that.

Other commonly used scripts and command:

# in package.json
"scripts" : {
    "server": "live-server"
}

# run with command
npm run server

Note: - We can use any keyword as key (eg the key we used above is “server”). - Unless the keyword we used as key are one of (start, test), then the command need to include run eg npm run {key}.

About version controls

image.png image-2.png

  • ^ (read as caret) eg ^4.17.21: will install the most recent minor version. Eg if most recent minor version is 4.18.0, then it will install that.
  • ~ (read as tilde) eg ~4.17.21 : will install the most recent patch version. Eg if most recent patch version is 4.17.22, then it will install that.
  • *: will install the most updated version. This might not be a good idea because the new major version might break our code.
  • If we did not include any of above symbols, it will install that exact version.

Tips

  • When uploading into git repository, you should only include package.json file and not include the ./node_modules folder. You should include this in your .gitignore. This is because the ./node_modules just stores all the 3rd party libraries you installed for your project.

References

  1. Gist/npmcrashcourse.txt
  2. Traversy Media/NPM Crash Course