Q1. What is package.json?
package.json is the manifest file for Node.js projects. It contains metadata about the project: name, version, description, entry point, scripts, dependencies, devDependencies, and more. It's created with npm init and is essential for managing project dependencies and sharing projects with others.
Q2. What are the required fields in package.json?
For publishing to npm, name and version are required. name must be lowercase, one word, can contain hyphens. version follows semantic versioning. Other common fields: description (helps in search), main (entry file), scripts (command shortcuts), keywords (for discoverability), author, license, and dependencies.
Q3. What are npm scripts in package.json?
Scripts are commands that can be run with npm run . Example:
"scripts": { "start": "node app.js", "dev": "nodemon app.js" }. You can run npm start (special) or npm run dev. Scripts can chain commands, access node_modules binaries, and are cross-platform.Q4. What is the purpose of the main field?
The main field specifies the entry point of your package. When someone requires your package (const pkg = require('your-package')), this file is loaded. Default is index.js. For applications, it's less important, but for libraries, it's crucial to define what gets exported.
Q5. How do you create a package.json file?
Run npm init in your project directory. It asks interactive questions to generate package.json. For default values, use npm init -y or npm init --yes. You can also set defaults with npm config set init-author-name "Your Name". The file should be committed to version control.
