npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lazy-node/is-writeable-path

v1.0.21

Published

路徑可寫入檢測模組 - 檢測目標路徑是否可寫入,支援同步和非同步操作,可檢測檔案和目錄的可寫入狀態

Readme

@lazy-node/is-writeable-path - 路徑可寫入檢測模組

這個模組提供了檢測目標路徑是否可寫入的功能,支援同步和非同步操作,可以檢測檔案和目錄的可寫入狀態。

主要功能

  • 檢測路徑是否可寫入(同步和非同步)
  • 檢測檔案是否可寫入
  • 檢測目錄是否可寫入
  • 支援 Node.js 檔案系統權限檢查

安裝

yarn add @lazy-node/is-writeable-path
yarn-tool add @lazy-node/is-writeable-path
yt add @lazy-node/is-writeable-path

快速開始

import { isWritableSync, isWritableAsync } from '@lazy-node/is-writeable-path';

// 同步檢測
const isWritable = isWritableSync('/path/to/file');
console.log('路徑可寫入:', isWritable);

// 非同步檢測
const isWritableAsync = await isWritableAsync('/path/to/file');
console.log('路徑可寫入:', isWritableAsync);

API 文件

基本檢測函數

isWritableAsync(target: PathLike): Promise

非同步檢測路徑是否可寫入。

參數:

  • target (PathLike): 要檢測的路徑

返回值:

  • Promise<boolean>: 檢測結果,true 表示可寫入,false 表示不可寫入

isWritableSync(target: PathLike): boolean

同步檢測路徑是否可寫入。

參數:

  • target (PathLike): 要檢測的路徑

返回值:

  • boolean: 檢測結果,true 表示可寫入,false 表示不可寫入

檔案檢測函數

isWritableFileAsync(target: PathLike): Promise

非同步檢測檔案是否可寫入。

參數:

  • target (PathLike): 要檢測的檔案路徑

返回值:

  • Promise<boolean>: 檢測結果

isWritableFileSync(target: PathLike): boolean

同步檢測檔案是否可寫入。

參數:

  • target (PathLike): 要檢測的檔案路徑

返回值:

  • boolean: 檢測結果

目錄檢測函數

isWritableDirectoryAsync(target: PathLike): Promise

非同步檢測目錄是否可寫入。

參數:

  • target (PathLike): 要檢測的目錄路徑

返回值:

  • Promise<boolean>: 檢測結果

isWritableDirectorySync(target: PathLike): boolean

同步檢測目錄是否可寫入。

參數:

  • target (PathLike): 要檢測的目錄路徑

返回值:

  • boolean: 檢測結果

使用範例

基本使用

import { isWritableSync, isWritableAsync } from '@lazy-node/is-writeable-path';

// 檢測檔案是否可寫入
const filePath = '/path/to/myfile.txt';
const canWrite = isWritableSync(filePath);
console.log(`檔案 ${filePath} 是否可寫入: ${canWrite}`);

// 檢測目錄是否可寫入
const dirPath = '/path/to/mydirectory';
const canWriteDir = isWritableSync(dirPath);
console.log(`目錄 ${dirPath} 是否可寫入: ${canWriteDir}`);

非同步使用

import { isWritableAsync } from '@lazy-node/is-writeable-path';

async function checkWriteable() {
  try {
    const canWrite = await isWritableAsync('/path/to/file');
    console.log('路徑可寫入:', canWrite);
  } catch (error) {
    console.error('檢查失敗:', error);
  }
}

checkWriteable();

檢測特定類型

import { 
  isWritableFileAsync, 
  isWritableFileSync,
  isWritableDirectoryAsync,
  isWritableDirectorySync 
} from '@lazy-node/is-writeable-path';

// 檢測檔案
const isFileWritable = isWritableFileSync('/path/to/file.txt');
console.log('檔案可寫入:', isFileWritable);

// 檢測目錄
const isDirWritable = isWritableDirectorySync('/path/to/directory');
console.log('目錄可寫入:', isDirWritable);

實際應用場景

import { isWritableSync } from '@lazy-node/is-writeable-path';
import { writeFileSync } from 'fs';

function safeWriteFile(filePath: string, content: string) {
  // 先檢查路徑是否可寫入
  if (!isWritableSync(filePath)) {
    throw new Error(`路徑 ${filePath} 不可寫入`);
  }

  // 安全地寫入檔案
  writeFileSync(filePath, content);
  console.log(`成功寫入檔案: ${filePath}`);
}

// 使用範例
try {
  safeWriteFile('/path/to/output.txt', 'Hello, World!');
} catch (error) {
  console.error('寫入失敗:', error.message);
}

錯誤處理

模組會自動處理以下情況:

  1. 路徑不存在:返回 false
  2. 權限不足:返回 false
  3. 非檔案/目錄類型:根據檢測類型返回 false

注意事項

  • 模組使用 Node.js 的 fs.access()fs.accessSync() 進行權限檢查
  • 檢測結果基於當前程序的執行權限
  • 在 Windows 系統上,某些特殊檔案名稱可能需要特殊處理

貢獻

歡迎提交問題和拉取請求!

授權

ISC