Reference article · Programming
Node.js and npm: project scripts and dependencies
Node.js runs JavaScript outside the browser for servers, terminal programs and development tools. npm manages packages and runs project-defined scripts.
Browser and Node.js environments
Both execute JavaScript but expose different APIs. document and the DOM belong to browsers; Node.js provides facilities such as filesystem APIs. A DOM example cannot run directly as a Node.js script without an additional DOM environment.
package.json and scripts
package.json describes package configuration. scripts assigns names to commands. In a practice folder, create this file and saludo.js containing console.log("Hello"). Run npm run saludo.
{
"name": "library-example",
"private": true,
"scripts": {
"saludo": "node saludo.js"
}
}Runtime and development dependencies
dependencies declares packages needed to use the distributed project. devDependencies commonly holds tests and build tools. A deployed application may need those tools during its build even if the final output does not use them at runtime.
install, lockfile and ci
npm install can update package-lock.json, which records the resolved dependency tree. npm ci requires a lockfile, fails on a package.json mismatch and removes node_modules before installation. It installs the whole project, not an individual new package.
node --version npm --version npm run saludo # In a project with a compatible package-lock.json: npm ci
Before running someone else’s project
Read scripts and Node.js requirements. Package installation and scripts can execute code; use known sources. Preserve the lockfile and review team and CI installation before changing package managers.
Practice
- Run saludo and verify Hello is printed.
- Rename the file without changing the script and observe the path error.
- Fix the script and check the correction addresses that error.
References
Related articles
- Fundamentals: variables, conditions and loops
- Semantic HTML and page structure
- CSS: box model, Flexbox and Grid
- JavaScript: functions, parameters and scope
- JavaScript: arrays, objects and data transformations
- DOM and events: connect JavaScript to HTML
- Promises, async/await and fetch requests
- HTTP and web APIs: requests, responses and status codes
- JSON: format, parsing and data validation
- Git: changes, commits, branches and remotes
- Debugging and tests: finding and checking errors
- Colour formats: HEX, RGB and HSL
- UUID identifiers
- Random values in JavaScript
