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. contract 在 node 22 · linux alpine/x64 …

sha256:022d81be2eae7cb4b1e1a75dd3f647a6db0aab22160d2d13daf5f1a846e8fda0

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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.');

原始种子者

匿名