An open API service indexing awesome lists of open source software.

https://github.com/dereknongeneric/project516

See nodejs/modules#516
https://github.com/dereknongeneric/project516

Last synced: 4 months ago
JSON representation

See nodejs/modules#516

Awesome Lists containing this project

README

          

# project516

Test fixture for `Module.resolvePackageRoot`. See [nodejs/modules#516](https://github.com/nodejs/modules/issues/516).

## Definitions


Module.resolvePackageRoot

Allows users to determine the package root of a package specifier


package root

the directory containing the package.json that served as the module's entrypoint

## Syntax

```mjs
Module.resolvePackageRoot(specifier);
Module.resolvePackageRoot(specifier, base);
```

If `base` is omitted, the equivalent of [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) will be used.

## Examples

Omitting `base` and getting the `package.json` of a package specifier. Hypothetical minimal example.

```mjs
import module from 'module';
import path from 'path';

const tapePkgRoot = module.resolvePackageRoot('tape');
const tapePjsonPath = path.resolve(tapePkgRoot, 'package.json');
const tapePjsonData = await import(tapePjsonPath); // :p

console.log(tapePjsonData);
```

Providing `base` and getting the `package.json` of a package (bare) specifier. Example that _could_ work today.

```mjs
import { promises as fs } from 'fs';
import module from 'module';
import path from 'path';

const lodashPkgRoot = module.resolvePackageRoot('lodash', import.meta.url);
const lodashPjsonPath = path.resolve(lodashPkgRoot, 'package.json');
const lodashPjsonData = JSON.parse(await fs.readFile(lodashPjsonPath, 'utf-8')); // :/

console.log(lodashPjsonData);
```

Omitting `base` and getting the `package.json` of a package (bare) specifier in only a few lines.

```mjs
import { resolvePackageRoot } from 'module';
import { join } from 'path';

const jestPjsonData = await import(
join(resolvePackageRoot('jest'), 'package.json'),
{ assert { type: 'json' } }); // !

console.log(jestPjsonData);
```

## Codebase layout

```tree
/app/
├── /node_modules/
│ └── /cjs-logger/
│ │ └── package.json
│ └── /esm-logger/
│ │ └── package.json
│ └── /logger/
│ ├── /cjs/
│ │ ├── logger.js
│ │ └── package.json
│ ├── /esm/
│ │ ├── logger.js
│ │ └── package.json
│ └── package.json
└── package.json
```

| /app/ | |
| --- | --- |
| `/app/node_modules/logger/package.json` | package boundary at `/app/node_modules/logger` |
| `/app/node_modules/logger/esm/package.json` | package boundary at `/app/node_modules/logger/esm` , in theory can contain `"exports"` |
| `/app/node_modules/logger/cjs/package.json` | package boundary at `/app/node_modules/logger/cjs` , in theory can contain `"exports"` |

## Further reading

- [📦 `realize-package-specifier`](https://www.npmjs.com/package/realize-package-specifier)
- [📦 `npm-package-arg`](https://www.npmjs.com/package/npm-package-arg)