[[oktatas:web:nodejs|< Node.js]] ====== package.json ====== * **Szerző:** Sallai András * Copyright (c) 2022, Sallai András * Szerkesztve: 2022, 2023, 2024 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== Készítés ===== npm init -y yarn init -y Használható a -y és --yes kapcsoló is. A pnpm nem szükséges a -y kapcsoló, mivel nem interkatív: pnpm init A yarn által létrehozott packages.json: { "name": "app01", "version": "1.0.0", "main": "index.js", "license": "MIT" } A package.json kézzel is elkészíthető. ===== A packages.json mezők ===== ==== name ==== Kötelezően: * egy szó * kisbetűs * kisebb vagy egyenlő mint 214 karakter Példa { "name": "app01" } ==== version ==== Kötelező felépítés: * major.minor.patch Példa: { "version": "1.0.0" } ==== Leírás ==== A projekt rövid leírása. Példa: { "description": "Rövid leírás a projektről" } ==== main ==== A projekt belépésipontja. Példa: { "main": "./src/index.js" } ==== scripts ==== Script parancsok megadása. { "scripts": { "test": "jest", "deploy": "gh-pages -d build" } } { "scripts": { "test": "jest", "predeploy":"valami elött", "deploy": "valami", "postdeploy": "valami utólag" } } Scriptet hívó scriptek: { "name": "zold.lan", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "npx copyfiles -u 1 src/**/*.{html,css} dist", "postbuild": "npm run build:bscss && npm run build:bsjs", "build:bscss": "npx copyfiles -u 4 node_modules/bootstrap/dist/css/bootstrap.css dist", "build:bsjs": "npx copyfiles -u 4 node_modules/bootstrap/dist/js/bootstrap.js dist", "deploy": "node vinyl-ftp.js" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "copyfiles": "^2.4.1", "vinyl-fs": "^3.0.3", "vinyl-ftp": "^0.6.1" }, "dependencies": { "bootstrap": "^5.2.3" } } ===== Több script ===== Egyetlen paranccsal szeretnénk több scriptet futtatni. Használjuk az npm-run-all parancsot. npm install --save-dev npm-run-all Leírás: * https://www.npmjs.com/package/npm-run-all (2023) "build": "npm-run-all build:src build:bs" { "name": "app01", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build:src": "npx copyfiles -u 1 src/**/*.{html,css} dist", "build:bs": "npx copyfiles -u 4 node_modules/bootstrap/dist/css/bootstrap.css dist", "build": "npm-run-all build:src build:bs" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "bootstrap": "^5.2.3" }, "devDependencies": { "copyfiles": "^2.4.1", "npm-run-all": "^4.1.5" } } A build:src és a build:bs futtatása egymás után: npm run build Ha párhuzamosan szeretnénk futtatni a két feladatot, akkor javítsuk a package.json fájlban: "build": "npm-run-all --parallel build:src build:bs" ===== Program külön parancsablakban ===== { "scripts": { "start": "lite-server" } } Külön parancsablakban Windowson: { "scripts": { "start": "start lite-server" } } ===== Linkek ===== * https://docs.npmjs.com/creating-a-package-json-file (2022) * https://codesweetly.com/package-json-file-explained (2022) * https://nodesource.com/blog/the-basics-of-package-json (2022) * https://heynode.com/tutorial/what-packagejson/ (2022) * https://flaviocopes.com/package-json/ (2022) * https://github.com/heroku/node-js-sample/blob/master/package.json (2022) * https://phoenixnap.com/kb/package-json (2022) * https://www.geeksforgeeks.org/node-js-package-json/ (2022)