Home > Web Front-end > JS Tutorial > npm and everything you need to know about the package.json

npm and everything you need to know about the package.json

Linda Hamilton
Release: 2024-11-28 00:57:12
Original
334 people have browsed it

npm and everything you need to know about the package.json

What is npm?

npm is a package manager for NodeJS. It is also the largest single language code repository on earth and a tool for installing and managing packages from the repository on the command line.

What is a package?

The npm registry consists of numerous packages or libraries that can be downloaded, installed, and used as a dependency in a NodeJS project. An npm package is a reusable piece of code published to the npm registry. It helps developers improve their workflow by incorporating functionality, thereby reducing the need to write redundant or repetitive code.

How do I install a package in my NodeJS project?

By using the CLI command npm install

  • npm install: This command will install all the dependencies mentioned in the package.json in the node_modules folder.
  • npm install : Installs the package in the current project directory (inside the node_modules folder). The package is accessible only within that project.
  • npm install -g : Installs the package system-wide, making it available from anywhere on your machine.
  • npm install @: Installs a specific version of that package.
  • npm install --save-dev: Installs the package and puts it in the devDependencies block of package.json
  • npm install --no-save: Installs the package but does not add the entry to the package.json file dependencies.
  • npm install --save-optional: Installs the package and adds the entry to the package.json file's optionalDependencies
  • npm install --no-optional: This will prevent the installation of optional dependencies.

What is package.json?

package.json is a configuration file used in Node.js projects to manage project metadata, dependencies, and scripts. It acts as the heart of a NodeJS project.

What is the difference between devDependencies and peerDependencies?

devDependencies: These are packages and libraries needed only during development or testing. They are not included in the production code.

Installation:

npm install tslint --save-dev

peerDependencies: These are dependencies that the project needs to work on, but it expects the user who is installing the package to provide the dependency.

"peerDependencies": {
  "graphql": ">=10.0.0"
}
Copy after login
Copy after login

The above block means:

  • The project needs the package graphql to work.
  • It needs the version of the graphql package to be 10.0.0 or higher.
  • The package users must install GraphQL themselves.

Scripts in package.json

The scripts field in package.json defines commands that can be run using npm run . Some scripts worth mentioning:

start: The command to start the application.

"peerDependencies": {
  "graphql": ">=10.0.0"
}
Copy after login
Copy after login

build: Used for production builds.

"start": "node index.js"
Copy after login

test: Runs the unit test suite.

"build": "webpack --mode production"
Copy after login

dev: Starts the development server.

"test": "nyc"
Copy after login

lint: Runs a linter to check code quality.

"dev": "nodemon index.js"
Copy after login

clean: Cleans up build artefacts.

"lint": "tslint ."
Copy after login

compile: Used to transpile source code into a different format (e.g., TypeScript to JavaScript)

"clean": "rm -rf dist"
Copy after login

publish: Used to publish the package to a registry like npm.

"compile": "tsc"
Copy after login
  • Pre/Post Hooks: There are also pre/post hooks for scripts like preinstall, postinstall, prebuild, precompile, postpublish etc.

  • Custom scripts: Custom scripts can also be written in the package.json and can be just run using npm run

The above is the detailed content of npm and everything you need to know about the package.json. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template