CodeSampleX

サンプル

resolve-cwd 3.0.0: resolve module path from current working directory or return undefined silently

検証済みサンプル — npm resolve-cwd 3.0.0: resolve module path from current working directory or return undefined silently. node 22 · linux alpine/x64 · docker で…

sha256:022d81be2eae7cb4b1e1a75dd3f647a6db0aab22160d2d13daf5f1a846e8fda0

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

ケース

HOW
ゴール
resolve module path from current working directory or return undefined silently
パッケージ
シンボル
  • resolveCwd
  • resolveCwd.silent
環境
node 22.23.2
作成日
2026-08-27T21:37:48Z

コントラクト

  1. assert resolveCwd resolves existing module path relative to current working directory
  2. assert resolveCwd resolves installed package module from current working directory
  3. assert resolveCwd throws MODULE_NOT_FOUND error when target module does not exist
  4. assert resolveCwd.silent returns absolute path for existing module relative to current working directory
  5. assert resolveCwd.silent returns undefined without throwing for non-existent module

ファイル

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: resolve module path from current working directory or return undefined silently
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/resolve-cwd@3.0.0
Demonstrate these symbols/APIs:
  - resolveCwd
  - resolveCwd.silent

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:de79658d54cf23daa19731dfd35f04b4cb14452b3e1f11ef3925758ce4fcbdf5","contract":["assert resolveCwd resolves existing module path relative to current working directory","assert resolveCwd resolves installed package module from current working directory","assert resolveCwd throws MODULE_NOT_FOUND error when target module does not exist","assert resolveCwd.silent returns absolute path for existing module relative to current working directory","assert resolveCwd.silent returns undefined without throwing for non-existent module"],"goal":"resolve module path from current working directory or return undefined silently","kind":"HOW","packages":["pkg:npm/resolve-cwd@3.0.0"],"schemaVersion":1,"symbols":["resolveCwd","resolveCwd.silent"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/resolve-cwd@3.0.0"],"schemaVersion":1,"subject":"pkg:npm/resolve-cwd@3.0.0","symbols":["resolveCwd","resolveCwd.silent"],"verifierAdapter":"node-typescript@1"}
index.js
const resolveCwd = require('resolve-cwd');

/**
 * Resolves the path of a module from the current working directory.
 * Throws MODULE_NOT_FOUND error if the module cannot be resolved.
 *
 * @param {string} moduleId - The module identifier or relative path to resolve.
 * @returns {string} The resolved absolute file path.
 */
function resolveFromCurrentDir(moduleId) {
  return resolveCwd(moduleId);
}

/**
 * Resolves the path of a module from the current working directory silently.
 * Returns undefined instead of throwing if the module cannot be resolved.
 *
 * @param {string} moduleId - The module identifier or relative path to resolve.
 * @returns {string | undefined} The resolved absolute file path or undefined.
 */
function resolveFromCurrentDirSilent(moduleId) {
  return resolveCwd.silent(moduleId);
}

module.exports = {
  resolveFromCurrentDir,
  resolveFromCurrentDirSilent,
  resolveCwd,
};
package-lock.json
{
  "name": "sample-resolve-cwd",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-resolve-cwd",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "resolve-cwd": "3.0.0"
      }
    },
    "node_modules/resolve-cwd": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/resolve-cwd/-/resolve-cwd-3.0.0.tgz",
      "integrity": "sha512-OrZaX2Mb+rJCpH/6CpSqt9xFVpN++x01XnN2ie9g6P5/3xelLAkXWVADpdz1IHD/KFfEXyE6V0U01OQ3UO2rEg==",
      "license": "MIT",
      "dependencies": {
        "resolve-from": "^5.0.0"
      },
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/resolve-from": {
      "version": "5.0.0",
      "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz",
      "integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==",
      "license": "MIT",
      "engines": {
        "node": ">=8"
      }
    }
  }
}
package.json
{
  "name": "sample-resolve-cwd",
  "version": "1.0.0",
  "description": "Clean-room sample demonstrating resolve-cwd module resolution from current working directory",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "resolve-cwd": "3.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "resolve module path from current working directory or return undefined silently",
  "kind": "HOW",
  "packages": [
    "pkg:npm/resolve-cwd@3.0.0"
  ],
  "symbols": [
    "resolveCwd",
    "resolveCwd.silent"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import path from 'node:path';
import resolveCwd from 'resolve-cwd';
import { resolveFromCurrentDir, resolveFromCurrentDirSilent } from '../index.js';

// 1. Verify resolveCwd resolves an existing relative file from the current working directory
const expectedPackageJsonPath = path.resolve(process.cwd(), 'package.json');
const resolvedPkg = resolveCwd('./package.json');
assert.strictEqual(resolvedPkg, expectedPackageJsonPath, 'resolveCwd must resolve relative package.json to its absolute path');

const helperResolvedPkg = resolveFromCurrentDir('./package.json');
assert.strictEqual(helperResolvedPkg, expectedPackageJsonPath, 'resolveFromCurrentDir must resolve relative package.json to its absolute path');

// 2. Verify resolveCwd resolves an installed package from cwd
const resolvedPackage = resolveCwd('resolve-cwd');
assert.strictEqual(typeof resolvedPackage, 'string', 'resolveCwd must resolve installed package to a string path');
assert.ok(resolvedPackage.includes('resolve-cwd'), 'Resolved path must contain package name');

// 3. Verify resolveCwd throws MODULE_NOT_FOUND error when the module does not exist
assert.throws(
  () => {
    resolveCwd('./non-existent-module-xyz.js');
  },
  (err) => {
    assert.strictEqual(err.code, 'MODULE_NOT_FOUND', 'resolveCwd must throw error with code MODULE_NOT_FOUND for missing module');
    return true;
  },
  'resolveCwd must throw MODULE_NOT_FOUND for non-existent module'
);

assert.throws(
  () => {
    resolveFromCurrentDir('./non-existent-module-xyz.js');
  },
  (err) => {
    assert.strictEqual(err.code, 'MODULE_NOT_FOUND', 'resolveFromCurrentDir must throw error with code MODULE_NOT_FOUND for missing module');
    return true;
  },
  'resolveFromCurrentDir must throw MODULE_NOT_FOUND for non-existent module'
);

// 4. Verify resolveCwd.silent returns absolute path for an existing relative file
const silentResolvedPkg = resolveCwd.silent('./package.json');
assert.strictEqual(silentResolvedPkg, expectedPackageJsonPath, 'resolveCwd.silent must resolve existing relative module');

const helperSilentResolvedPkg = resolveFromCurrentDirSilent('./package.json');
assert.strictEqual(helperSilentResolvedPkg, expectedPackageJsonPath, 'resolveFromCurrentDirSilent must resolve existing relative module');

// 5. Verify resolveCwd.silent returns undefined without throwing for non-existent module
const silentMissing = resolveCwd.silent('./non-existent-module-xyz.js');
assert.strictEqual(silentMissing, undefined, 'resolveCwd.silent must return undefined for non-existent module');

const helperSilentMissing = resolveFromCurrentDirSilent('./non-existent-module-xyz.js');
assert.strictEqual(helperSilentMissing, undefined, 'resolveFromCurrentDirSilent must return undefined for non-existent module');

console.log('All contract assertions passed.');

オリジンシーダー

匿名