mongodb-api-lib
v2.0.0
Published
Advanced MongoDB & Express (Starting Project)
Downloads
5
Readme
Node.js v22.3.0 documentation
Node.js® is a free, open-source, cross-platform JavaScript runtime environment that lets developers create servers, web apps, command line tools and scripts.
Downloads Node.js v20.15.01 with long-term support. Node.js can also be installed via package managers. Want new features sooner? Get Node.js v22.3.01 instead.
(Node.js v22.3.0 docs) [https://nodejs.org/docs/latest/api/]
NPM Commands
node -v - get node version
npm -v - get npm version
npm init - initialize nodejs project
npm init --yes - package.json file with default values
npm install - install a package
npm i - install a package
npm list - list of all dependencies & sub dependencies
npm list --debth=0 - list of all dependencies of local application
npm ls - list of all dependencies & sub dependencies
npm ls --debth=0 - list of all dependencies of local application
npm view mongoose - list registry information of a package
npm view mongoose dependencies - look at values of the dependencies property
npm view mongoose versions - list all mongoose version
npm install [email protected] - install specific dependency version, ex. [email protected]
npm outdated - show all outdated packages
npm -g outdated - show all outdated global packages
npm update - updates minor and patch releases, does not update to latest major version
sudo npm i -g npm-check-updates - show all the outdated packages and their new version.
ncu -u - show all the outdated packages and their new version.
npm i jshint --save-dev - install a development dependency
npm uninstall - uninstall package
npm install -g [package] - install globally
Semantic Versioning
- Major.Minor.Patch
Update Version via CL
- npm version major
- npm version minor
- npm version patch
"mongose": "^4.13.6" carot - ^ dependency on any version of mongoose as long as major version is 4 / 4.x
"underscore": "~1.13.1" tilde - ~ dependency on any versions of underscore as long as major version is 1 and minor version is 13 / 1.13.x
/* Semantic Versioning
"dependencies": {
"mongoose": "^8.4.4", // Major.Minor.Patch
"mongoose": "^8.5.0", // (Mongoose Team adds a new feature without breaking existing API: increase the minor version)
"mongoose": "^9.0.0", // (Mongoose Team adds a new feature that can break existing API: increase the major version)
"mongoose": "^9.0.0", // (Mongoose Team fixes or patches a bug: increase the patch version)
"mongoose": "^8.4.4", // ^ - specifies any newer version as long as the major version is 8.x.x
"mongoose": "^8.4.4", // ~ - specifies any newer version as long as the major version is 8 and minor version is 4, or (8.4.x)
"mongoose": "8.4.4", // 8.4.4 - specifies the exact version newer version
"underscore": "^1.13.6"
}
*/
Publishing a Package
npm login - enter user name / password / email
npm publish - publish package to npm *change the package name ["name": "mongodb-api"]
Install Published Package In a new dir lib-test install the newly create package ["mongodb-api"] npm install --save-dev whskygolf-lib
Publish newer Version of Package
- update the version number.
- Add new feature - make changes ot src. npm version minor - update minor version # npm publish
Show all outdated packages
sudo npm i -g npm-check-updates - show all the outdated packages and their new version.
ncu -u
P_rettier_ Install Prettier
npm install --save-dev --save-exact prettier
Guide To install Prettier locally in your project directory: Ensures everyone in the project gets the exact same version of Prettier.
1. Add a .prettierrc.json to let your editor know that you are using Prettier.
2. Add a .prettierignore to let your editor know which files not to touch, as well as for being able to run prettier --write . to format the entire project (without mangling files you don’t want, or choking on generated files).
3. Run prettier --check . in CI to make sure that your project stays formatted.
4. Run Prettier from your editor for the best experience.
5. Use eslint-config-prettier to make Prettier and ESLint play nice together.
6. Set up a pre-commit hook to make sure that every commit is formatted.
7. Optiojnal: Install the editor extension - “editor.formatOnSave”: true
whenever you save your code, it will be automatically formatted.
Configurew pr-commit hooks
Pre-commit hooks are a great way to run certain checks to ensure clean code. This can be used to format staged files if for some reason they weren’t automatically formatted during editing. husky can be used to easily configure git hooks to prevent bad commits. We will use this along with pretty-quick to run Prettier on our changed files. Install these packages, along with npm-run-all, which will make it easier for us to run npm scripts:
npm install npm-run-all husky pretty-quick -D
To configure the pre-commit hook, simply add a “precommit” npm script. We want to first run Prettier, then run TSLint on the formatted files. To make our scripts cleaner, I am using the npm-run-all package, which gives you two commands, run-s to run scripts in sequence, and run-p to run scripts in parallel:
"format:fix": "pretty-quick --staged",
"precommit": "run-s format:fix lint",
"lint": "ng lint",
Now, when you commit your staged files, it will first format your files and then run those through tslint.
Configure npm script for CI Last thing we need to do is to add a check that we can use in a CI environment. In this particular case, we should check our source files and error out if any files fail to conform to Prettier’s configuration. We can configure a “format:check” npm script for this:
{
"format:check": "prettier --config ./.prettierrc --list-different \"src/{app,environments,assets}/**/*{.ts,.js,.json,.css,.scss}\""
}
(Prettier) [https://prettier.io/docs/en/install.html]
Templating Engines
Templating engines generate dynamic HTML which is returned to client.
- Pug (Previously Jade)
- Mustache
- EJS
Install Template Engine With NPM
- npm i pug
Set The View Engine In The App
- app.set('view engine', 'pug')
- app.set('views', './views') // default