Fundamentals 14 min read

Mastering Inodes, Hard & Soft Links: From Linux to Frontend Tooling

This article explains the fundamentals of inodes, sectors, and blocks, demonstrates how to retrieve file information with Node.js and Linux commands, compares hard and soft links, and shows practical applications of these links in frontend workflows such as yarn link and pnpm installation.

ELab Team
ELab Team
ELab Team
Mastering Inodes, Hard & Soft Links: From Linux to Frontend Tooling

Prerequisite Knowledge

First understand the important operating‑system concept inode (index node).

inode

inode is a file data structure used to store all Linux file information except the name and data. More details can be found at the referenced article.

Sector: the smallest storage unit on a hard disk, typically 512 bytes.

Block: multiple sectors form a block, commonly 4 KB (8 sectors); blocks are the minimal unit for file access.

inode: stores metadata such as creator, creation date, size, etc. The system uses the inode number to identify files, not the filename.

Node method to get file information

const fs = require('fs');
const fileInfo = fs.statSync('./src/logo.svg');
console.log('文件信息:', fileInfo);

Output example:

文件信息:Stats {
  dev: 16777220,
  mode: 33188,
  nlink: 1,
  uid: 501,
  gid: 20,
  rdev: 0,
  blksize: 4096,
  ino: 114457648, // inode unique identifier
  size: 2632,
  blocks: 8,
  atimeMs: 1639292681311.895,
  mtimeMs: 1638968536815.0586,
  ctimeMs: 1638968536815.1282,
  birthtimeMs: 1638968536814.7827,
  atime: '2021-12-12T07:04:41.312Z',
  mtime: '2021-12-08T13:02:16.815Z',
  ctime: '2021-12-08T13:02:16.815Z',
  birthtime: '2021-12-08T13:02:16.815Z'
}

Linux command to get inode

Use ls -i to list files with their inode numbers.

Soft Link (Symbolic Link) and Hard Link

Both hard and soft links do not duplicate the source file; they only occupy a tiny amount of disk space.

Hard Link

Multiple files can point to the same inode.

Deleting one file does not affect the others; modifications are reflected across all links.

Can only create hard links for files, not directories.

Typical use case: mirroring data files to prevent accidental deletion.

Create Hard Link

Use a Linux command to create a hard link logoHard.svg that points to logo.svg.

View Hard Link Information

const fs = require('fs');
const fileInfo = fs.statSync('./src/logo.svg');
console.log('logo文件信息:', fileInfo);
const fileInfoHardLink = fs.statSync('./src/logoHard.svg');
console.log('logo硬链接文件信息:', fileInfoHardLink);

The output shows identical inode and other metadata; the link count (nlink) indicates how many names refer to the inode.

$ node link.js
logo文件信息:Stats { ... nlink: 2, ... }
logo硬链接文件信息:Stats { ... nlink: 2, ... }

Soft Link

Also called a symbolic link.

It is a link file that points to the source file's path, similar to a pointer.

Changing the source updates the soft link; deleting the source makes the soft link broken ( No such file or directory).

Create Soft Link

Linux command

Use ln -s logo.svg logoSoft.svg to create a symbolic link.

Node method

const fs = require('fs');
fs.symlinkSync('./src/logo.svg', './logoSoftV2.svg');

View Soft Link Information

Linux command

Use ls -il to display the inode and target of a symbolic link.

Node method

const fs = require('fs');
const fileInfo = fs.statSync('./src/logo.svg');
console.log('logo源文件信息:', fileInfo);
const logoSoftInfo = fs.lstatSync('./src/logoSoft.svg');
console.log('logo软链接文件信息:', logoSoftInfo);

The soft link has a different inode and a much smaller size because it only stores the link information.

$ node readSoftLink.js
logo源文件信息:Stats { ... ino: 114457648, size: 2632, ... }
logo软链接文件信息: Stats { ... ino: 114734506, size: 14, ... }

Differences Between Soft and Hard Links

Soft links have separate inodes, are link files, support cross‑filesystem linking, do not increase the link count, and become broken when the source is removed. Hard links share the same inode as the source, cannot span filesystems, increase the link count, and remain accessible after the original name is deleted. Typical uses: soft links for shortcuts, package linking (yarn, pnpm); hard links for backup, deduplication, and preventing accidental deletion.

Soft & Hard Links in Frontend Development

yarn link

During development, a local package can be linked to other projects without publishing, allowing immediate testing of changes.

In the development stage, the package can be used in other projects via yarn link for testing new features or debugging.

Usage

Example: run yarn link in package B, then yarn link @byted-ep/slardar in package A so that changes in B are reflected in A.

pnpm

Usage

When running pnpm install, pnpm stores actual packages in a central .pnpm store and creates soft links in node_modules to save disk space and speed up installation.

The .pnpm directory holds the real package files.

Other entries in node_modules are soft links pointing to the files in .pnpm.

Hard Link Usage

pnpm uses a global store (usually under ~/.pnpm-store ) and creates hard links so that identical packages are not downloaded multiple times; the same inode is shared across projects.

References

纠结的链接— ln, ln -s, fs.symlink, require

现代前端工程化— 链接的详细解释与应用

linux之软连接和硬连接的区别

一口气搞懂「文件系统」的 25 张图

pnpm 软链接和硬链接的实践与痛点解决

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

inodepnpmYARNFilesystemFrontend toolingHard Linksoft link
ELab Team
Written by

ELab Team

Sharing fresh technical insights

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.