With NVM: you can manage the NodeJs version with this system, so you don’t have to endure the installing and uninstalling process each time you work in a different project. There is also a NVM Windows installer.
Install a version
Download, compile, and install the latest release of node
1
nvm install node # "node" is an alias for the latest version
Set default global packages from file while installing
If you have a list of default packages you want installed every time you install a new version, just add the package names, one per line, to the file $NVM_DIR/default-packages.
npm i -S react redux react-redux lodash @material-ui/core
Install packages from different sources
Default source for npm install is the npm-registry
You can get it from a tarball or git
1 2 3 4 5 6 7 8
# from git service npm i https://github.com/username/myrepository.git npm i bitbucket:username/myrepository npm i gitlab:username/myrepository ## from GitHub: userName + from repo name (Redux) npm i username/myrepository ## from GitHub: from a branch npm i username/myrepository#develop
Linking Packages
Package linking works by creating a symlink in your node_modules folder that points to the local repository of your package. * * You can edit packages locally and the changes will be instantly available in the project using it.
1 2 3 4 5 6 7
# You have myPackage which will be used by myProject # Go to the package folder cd ./myPackage npm link # then go to the main project folder cd ../myProject npm link mypackage
The npx command
When using sciprting tools, such as Grunt, gulp, react-create-app, react-native-cli or mocha.
Before NPM 5.x you had to install them either as global packages or as devDependencies. This was time-consuming, it was time consuming.
npx is binary which downloads an installs automatically
1
npx create-react-app my-new-project
Monitor and clean your project
Scope: to solve issues and to make the dependency tree and the final bundle size as small as possible.
Check dependecy tree and which package versions have actually been installed.
1 2 3 4
# check dependency tree npm list # only top level + global packages npm list --depth=0 -g
dedupe your project before publishing.
1 2 3
npm dedupe # or you can use the shortcut npm ddp
Overview of your outdated and missing packages
1
npm outdated
3.1. If you get a lot of red rows, update to the latest possible version according to your package.json
1
npm update
3.2. If you are using the caret ‘^’ in front of your versions in package.json, the major versions will not be updated (hence the yellow rows). This is good, because there might be breaking changes updating to a new major version.
3.3. If you still want to update everything to the latest version, you should do it from the main folder
1
npx npm-update-all
Documentation from the terminal
Command
Action
npm repo
Open the repository for a package in the browser
npm home
Open the homepage
npm docs
Open the documentation
Use NPM scripts
Fewer dev-dependencies or global dependencies to maintain.
No new tools for contributors and team members to learn
Running NPM scripts on Terminal Now you can run npm run format and ESLint will do its job. You can get the list of available scripts via npm run
1 2 3 4
# get the list of available scripts npm run # run the script we created previously npm run format
Running NPM Scripts in VScodium On VSCodium, you can list all your npm-scripts in the explorer and run your scripts with a click of a button by enabling the option.
1
npm.enableScriptExplorer:true
Set your default values
Intialize the project and fill the default values:
1 2 3
npm config set init.author.name "Angeles Broullon" npm config set init.author.email "AngelesBroullon@mail.com" npm init -y
Setting up a Gitlab page with a NodeJS blogging system
I started a new project in order to generate a static web page, a small blog to store and share my programming notes and cheatsheets which I write by default in Markdown. There are several alternatives, but I chose to use Hexo since I wanted to try the new NodeJS version.
NVM: you can manage the NodeJs version with this system, so you don’t have to endure the installing and uninstalling process each time you work in a different project. There is also a NVM Windows installer.
❗ Note: while I prefer using the console when I’m not doing any major merge, you can always use a GUI Git system.
First I will download the latest NodeJs version via NPM and check it out.
1 2
nvm install nvm list
To get the code into my local system.
1
git clone https://gitlab.com/usuario/code-notepad
Then I get the Hexo command line interface via npm, the NodeJs default package manager.
1
npm install hexo-cli -g
And now, welcome to Hexo.
Creating the blog
First we initialize the blog.
1 2 3
hexo init blog cd blog npm install
Then if we want to have assets (e.g. images) for each post, we should set the right variable for that. It is stored in _config.yml.
1
post_asset_folder:true
And finally we deploy in single command to check everything works as we want to.
1
hexo server
Customization with themes and plugins
We may want to use a custom theme. It’s as easy as to go the repository, download it an store it in the themes folder.
I love mermaidJS diagrams, as they provide an easy way to create diagrams while taking notes, and they are portable. Hence I added a pluging to been able to use them in my posts via npm.
<% if (theme.mermaid.enable) { %> <scriptsrc='https://unpkg.com/mermaid@<%= theme.mermaid.version %>/dist/mermaid.min.js'></script> <script> if (window.mermaid) { mermaid.initialize({theme: 'forest'}); } </script> <% } %>
Adding the CI
Finally I added the CI (Continuous Integration) file, in the root of the directory: .gitlab-ci.yml. This file will use a docker container with a NodeJS 10.15.3 image, where it will install Hexo, build the packages, generate the pageand deploy the content in Gitlab pages.
With this we have the skeleton ready, so now we only need to upload the code to Gitlab, and that will automatically launch the process getting the page to something similar to this: https://angelesbroullon.gitlab.io/code-notepad/.
1 2
git commit -a -m "Init blog system" git push
How to blog
We can create posts from the source folder by writing them using Markdown syntaxn.
1 2
cdsource hexo new post "My firts post"
This will generate a markdown file with the following header, which will allow us to sort out the content.
1 2 3 4 5
--- title: My fist post date: 2018-04-17 10:00:00 tags: [Tag1, Tag2] ---
We can store images in the folder which was created with the same name as the post, using the following syntax (e.g. in the “My first post” folder we store an image image.jpg):
1 2
{% asset_img "image.jpg" "Image description " %}
Then we commit, push, and Gitlab will do its magic.