app • NodeJS package management - martindubenet/wed-dev-design GitHub Wiki
NodeJS includs NPM but these are two different applications. NPM is known to be the best package management system for javascript but it is not the only option for doing so. Yarn, Webpack are the latest trends.
node -v
npm -v
- Latest (LTS) version :
- Windows : NodeJS.org ➥ Download
- macOS via Homebrew :
brew install node
- Legacy versions via nvm app :
nvm use 14
You can also rely on « nvm » (Node Version Manager) to switch to different versions of node (and npm) for specific repositories on your workstation.
- Clear NPM cache
npm cache clean --force
- Delete contents
rm -rf node_modules package-lock.json
- Re-install
npm install
NVM allows you to quickly install and use different versions of node via the command line. Use command line « nvm ls-remote
» to list all the versions of NodeJs available to install.
nvm install 18
nvm use 18
## Now using node v18.19.0 (npm v10.2.3)
nvm use 14
## Now using node v14.18.0 (npm v6.14.15)
nvm use 18
## Now using node v18.19.0 (npm v10.2.3)
## You can confirm by looking for the node and/or npm version
node -v
npm -v
Type
nvm --help
to see all the options
-
To update to latest npm version :
npm install npm@latest -g
-
To launch NPM in a project :
npm init
« Best package management system for javascript », « Open-source » and « Great community » are the key factors why developers consider npm;
Whereas « Most powerful bundler », « Built-in dev server with livereload » and « Can handle all types of assets » are the primary reasons why Webpack is favored.
- NPM keeps track of all the packages and their versions and allows the developer to easily update or remove these dependencies.
- Yarn is a superset of NPM that solves many problems the later has.
- NPM vs Webpack
This example is for a new Wordpress Theme
cd /wp-content/theme/{MY_THEME}
npm init -y
npm install {-g} {PACKAGE_NAME}
npm install
Note: At the 2nd command line above, the «
-y
» stands for «answer yes to all questions»
List which package depend on a specific package
npm ls {PACKAGE_NAME}
Get an overview of your packages
npm audit
If you get an error message about Access Rights (on macOS probebly) copy/paste the following line. You can read more here : « How to fix the "Missing write access" error when using npm ».
sudo chown -R $USER /usr/local/lib/node_modules
If you get the following message from npm it simply means that you are missing {whatever}
in your « package.json➥devDependencies
». To fix this simply use the following command line syntaxe.
«
npm WARN {this} requires a peer of {whatever} but none is installed. You must install peer dependencies yourself.
»npm install --save-dev "{whatever}"
Both files are automatically generated but at different moment.
file | generated by | edit | description |
---|---|---|---|
package.json |
npm init |
manually |
The main file for npm to install Commands like |
package-lock.json |
npm install |
auto-generated |
NPM generates this child file based on what is configure in the parent NPM highly recommend to commit the generated package lock to (git) source control, so do NOT list this file in Note: |
By default a package is saved as a core dependency for the solution. Therefor it is expected as required on the server either for build scripts or simply for the solution to run on the servers.
If you don't need the package on the remote server for any reason; Example maybe your Sass files are compiled locally so only CSS are required on server. Then it is a good practice to set that package as a « save for development only » by adding the following
--save-dev
instruction at the end of the install command line of a package.
Normal | Shorthand | Definition |
---|---|---|
install |
i |
Self explanatory |
--global |
-g |
Global installation for all users of your workstation. May required sudo as prefix. |
--no-save |
Prevents saving to dependencies . |
|
--save |
List the package within dependencies . Project’s packages required by your application in production server. |
|
--save-dev |
-D |
List the package within devDependencies . This package is required for developers work stations ONLY. It is not required to run the solution on the server so no need to push on the servers.` |
--save-exact |
-E |
Saved dependencies will be configured with an exact version rather than using npm's default semver range operator. |
--save-prod |
-P |
Installs the package in the root directory. |
- Shorthands and Other CLI Niceties related to npm-config.
For versioning node_modules
listed in package.json
Rule | Definition |
---|---|
^ |
It will only do updates that do not change the leftmost non-zero number i.e there can be changes in minor version or patch version but not in major version. If you write ^13.1.0 , when running npm update, it can update to 13.2.0 , 13.3.0 even 13.3.1 , 13.3.2 and so on, but not to 14.0.0 or above. |
~ |
If you write ~0.13.0 when running npm update it can update to patch releases: 0.13.1 is ok, but 0.14.0 is not. |
> |
You accept any version higher than the one you specify. |
>= |
You accept any version equal to or higher than the one you specify. |
<= |
You accept any version equal or lower to the one you specify. |
< |
You accept any version lower than the one you specify. |
= |
You accept that exact version. |
- |
You accept a range of versions. Example: 2.1.0 - 2.6.2 . |
|| |
You combine sets. Example: < 2.1 || > 2.6 . |
- serve
- bootstrap
- cli-real-favicon generates a multiplatform favicon with RealFaviconGenerator.
- concat to Concatenate multiple files into one.
-
dart sass but install it globally :
npm install -g node-sass
. - eslint
- fontawesome
- npm-run-all
- prettier
-
webp-converter to convert
base64
,gif
,png
,jpg
image files to or from*.webp
. images.