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.
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.
By using the CLI command npm install
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.
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" }
The above block means:
The scripts field in package.json defines commands that can be run using npm run
start: The command to start the application.
"peerDependencies": { "graphql": ">=10.0.0" }
build: Used for production builds.
"start": "node index.js"
test: Runs the unit test suite.
"build": "webpack --mode production"
dev: Starts the development server.
"test": "nyc"
lint: Runs a linter to check code quality.
"dev": "nodemon index.js"
clean: Cleans up build artefacts.
"lint": "tslint ."
compile: Used to transpile source code into a different format (e.g., TypeScript to JavaScript)
"clean": "rm -rf dist"
publish: Used to publish the package to a registry like npm.
"compile": "tsc"
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!