fs.symlink(target, path[, type], callback)


创建名为 path 的链接,指向 target。除了可能的异常之外,不会向完成回调提供其他参数。

🌐 Creates the link called path pointing to target. No arguments other than a possible exception are given to the completion callback.

有关更多详细信息,请参阅 POSIX symlink(2) 文档。

🌐 See the POSIX symlink(2) documentation for more details.

type 参数仅在 Windows 上可用,其他平台会被忽略。它可以设置为 'dir''file''junction'。如果 type 参数为 null,Node.js 会自动检测 target 类型并使用 'file''dir'。如果 target 不存在,将使用 'file'。Windows 的连接点(junction points)要求目标路径为绝对路径。使用 'junction' 时,target 参数会自动规范为绝对路径。NTFS 卷上的连接点只能指向目录。

🌐 The type argument is only available on Windows and ignored on other platforms. It can be set to 'dir', 'file', or 'junction'. If the type argument is null, Node.js will autodetect target type and use 'file' or 'dir'. If the target does not exist, 'file' will be used. Windows junction points require the destination path to be absolute. When using 'junction', the target argument will automatically be normalized to absolute path. Junction points on NTFS volumes can only point to directories.

相对目标是相对于链接的父目录。

🌐 Relative targets are relative to the link's parent directory.

import { symlink } from 'node:fs';

symlink('./mew', './mewtwo', callback); 

上面的例子创建了一个符号链接 mewtwo,它指向同一目录下的 mew

🌐 The above example creates a symbolic link mewtwo which points to mew in the same directory:

$ tree .
.
├── mew
└── mewtwo -> ./mew