Update npm

npm i -g npm@VERSION

1
npm i -g npm@5.5.1

Create package.json

1
2
3
4
5
# Create package.json with interactive command prompts
npm init

# Create package.json without any command prompts
npm init --yes

List existing packages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# List dependency tree
npm list

# List dependency tree with depth=0
npm list --depth=0

# View package detail information
npm view mongoose

# View package dependencies
npm view mongoose dependencies

# View package all versions
npm view mongoose versions

Versioning

Semantic format: Major.Minor.Patch

  • Caret Version
    ^4.13.6=4.x

  • Tilde Version
    ~1.8.3=1.8.x

Update local package

1
2
3
4
5
6
7
8
# Check outdated packages
npm outdated

# Check global outdated packages
npm -g outdated

# Update packages
npm update

Use npm-check-updates to update packages

1
2
3
4
5
6
7
8
9
10
11
12
# 1. Install npm-check-updates globally
npm i -g npm-check-updates
# 2. Check which packages can be updated
npm-check-updates
# Or
ncu

# 3. Update package.json
ncu -u

# 4. Install dependencies
npm i

Install dev dependencies

1
npm i jshint --save-dev

Uninstall package

1
npm un mongoose

Publish own packages to npm

1
2
3
4
5
mkdir lion-lib
cd lion-lib
npm init --yes
npm login
npm publish

Why do we need it?

Generic allows developers create classes or methods work with any data type.

Let’s take a look at the following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//List.cs
using System;

namespace Generic
{
public class List
{
public void Add(int value)
{
throw new NotImplementedException();
}

public int this[int index]
{
get
{
throw new NotImplementedException();
}
}
}
}

Read More

If we don’t have enough memory in a Linux machine, we can enable the swap space to increase the “memory”.

Check the free memory and free space

1
2
3
4
5
6
7
8
#check if any swap space enable
sudo swapon --show

#verify there is no active swap
free -h

#check free space of the hard drive
df -h

Create the swap file and enable swap space

1
2
3
4
5
6
7
8
9
10
11
#create the swap file
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB

#make the file only accessible by root
sudo chmod 600 /swapfile

#mark the file as swap space
sudo mkswap /swapfile

#enable the swap file
sudo swapon /swapfile

Make the swap file permanent

1
2
3
4
5
#backup the /etc/fstab file in case anything goes wrong
sudo cp /etc/fstab /etc/fstab.bak

#add the swap file information to the end
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

That’s all for enabling the swap file on Linux. For more information, we can find more on https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04

I’ve seen quite a lot of PHP developers don’t have PHP debugger installed on development enviornment. Instead of debugging PHP code line by line, we’re fond of echo and print the results on the web page to find the errors.

It would be hard in the past because we don’t have good PHP IDE until we have PHPStorm. Unfortunately PHPStorm doesn’t have debugger installed in the environment and we have to install it by ourselves.

Download xdebug

1
git clone git://github.com/xdebug/xdebug.git

Install XDebug

1
2
3
4
5
cd xdebug
phpize
sudo ./configure –enable-xdebug
sudo make
sudo make install

NOTE: After the installation, it will output the path of debug.so from the command line. Save it and paste into zend_extension to the configuration setting in next step.

Paste the following code to php.ini

1
2
3
4
5
6
7
zend_extension=/usr/lib/php/20151012/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.idekey=PHPSTORM
xdebug.remote_port=9000
xdebug.remote_log=/var/log/xdebug.log

https://confluence.jetbrains.com/display/PhpStorm/Remote+debugging+in+PhpStorm+via+SSH+tunnel

Sometimes we need to enable the MySQL log to see what SQL scripts have been created by our application. This is a way to enable MySQL log.

  1. Login to mysql in command line
  2. Run the scripts below:
1
2
3
4
/* For Mac OS */
SET global log_output = 'FILE';
SET global general_log_file='/Applications/MAMP/logs/mysql_general.log';
SET global general_log = 1;
1
2
3
4
/* For Linux */
SET global log_output = 'FILE';
SET global general_log_file='/var/log/mysql/mysql_general.log';
SET global general_log = 1;

It can be turned off with:

1
SET global general_log = 0;

There are thousands of linux commands available. But there are only a few commands we use for most of the time. As a result, we don’t have to learn and recite all the commands. Of course we can check the manuals by “man” or google it but it’s not a fast way for me to search them all the time. Therefore, I list them all together as a cheat sheet.

System info

1
2
3
4
5
6
7
8
9
10
11
12
13
date    ## show the current date and time
cal ## show this month's calendar
uptime ## show current uptime
w ## display who is online
whoami ## who you are logged in as
finger user ## display information about user
uname -a ## show kernel information
man `command` ## show the manual for command
df ## show disk usage
du ## show directory
free ## show memory and swap usage
whereis app ## show possible location of app
which app ## show which app will be run by default

Read More

I’ve been working on PHP for more than 5 years time. As far as I can find, there are still a lot of companies using PHP with version lower than 5.6. Especially some e-commerce stores like Woocommerce, Magento 1.x. Most of the owners won’t upgrade PHP because the systems are still working fine. There’s no way to spend money on something unnecessary. As a result, developers should keep the PHP version in mind when we need to build new feature or do some bug fixes for the old system.

The current version of PHP is 7.1. But there are more than 50% of users are still using PHP 5.x[1] and some PHP versions are end of officially supported [2].

This article will introduce the new features from PHP 5.2 to PHP 5.6.

  • PHP 5.2 before: autoload, PDO and MySQLi, type of constraints
  • PHP 5.2: JSON support
  • PHP 5.3: Abandoned features, anonymous functions, new magic methods, namespace, late static binding, Heredoc, Nowdoc, const, ternary operator, Phar
  • PHP 5.4: Short Open Tag, new syntax of array, Traits, built-in web server, small features changes
  • PHP 5.5: yield, list() in foreach, feature changes
  • PHP 5.6: constant scalar expressions, Variadic functions and argument unpacking, namespace enhancement

Read More

I’ve been a software developer for more than 5 years time. Over the past few years, I have greatly improved my development and communication skills. As the technologies keep updating days by days, I always worry my programming knowledge won’t be enough. Sometimes I doubt if I have fallen behind the industry.

In order to find out what I should learn next for my career, I am in favour of joinging the technical meetups as much as I can. As far as I can find from the conversations during the meetups, I realised I had to get in the habit of writing blogs occasionally to record what I have learnt from work. Because it would be much easier to find out the same question from my blog than google it again.

I believe I can learn more from writing technical blogs than keep reading the new books with nothing else.

Therefore, I should keep posting at least once per week. Keep fighting. (^o^)