fpj
Resolves The Location Of The Package.Json File For The Given Dependency By Traversing The File System Up Starting From Given Path.
yarn add -E fpj
- Table Of Contents
- API
async fpj(dirname: string, packageName: string, opts?: FPJConfig): FPJReturn
- Fields
- Soft Mode
- Copyright
The package is available by importing its default function:
import fpj from 'fpj'
Returns the resolved entry point to the package. It will start checking for the presence of packages using Node's algorithm by resolving the node_modules
folder first inside of the given dirname, then if not found, traverse up and repeat, until root of the OS is reached.
The preference of the entry
output will be given to the module
field specified in the package.json. If the main
is found instead, it will be indicated with hasMain
property on the returned object.
_fpj.Config
: The options for fpj
.
Name | Type | Description | Default |
---|---|---|---|
fields | !Array<string> | Any additional fields from package.json file to return. |
- |
soft | boolean | If the entry export (main or module) does not exist, soft mode will not throw an error, but add the hasEntry property to the output set to false. |
false |
For example, the package.json
files and meta information for 2 packages can be fetched using the following example:
/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'
(async () => {
const zoroaster = await fpj(
dirname('example/example.js'),
'zoroaster'
)
console.log(zoroaster)
const read = await fpj(
dirname('example/example.js'),
'@wrote/read'
)
console.log(read)
})()
FPJ gives preference to the module
field and will report it as the entry if it exists. Otherwise, the main
is used with the hasMain
property set to true:
{
entry: 'node_modules\\zoroaster\\depack\\index.js',
packageJson: 'node_modules\\zoroaster\\package.json',
version: '4.3.0',
packageName: 'zoroaster',
hasMain: true
}
{
entry: 'node_modules\\@wrote\\read\\src\\index.js',
packageJson: 'node_modules\\@wrote\\read\\package.json',
version: '1.0.4',
packageName: '@wrote/read'
}
_fpj.Return
: The return type of the program.
Name | Type | Description |
---|---|---|
entry* | string | The location of the package's entry file. The preference is given to the module field. |
packageJson* | string | The path to the package.json file itself. |
packageName* | string | The name of the resolved package. |
version | string | The version of the package. |
hasMain | boolean | Whether the entry is the main rather than module . |
entryExists | boolean | In soft mode, will be set to false if the entry file does not exist. |
Any additional fields from package.json
that need to be present in the output can be specified in the fields
property.
/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'
(async () => {
const zoroaster = await fpj(
dirname('example/example.js'),
'zoroaster',
{ fields: ['license', 'bin'] },
)
console.log(zoroaster)
})()
{
entry: 'node_modules\\zoroaster\\depack\\index.js',
packageJson: 'node_modules\\zoroaster\\package.json',
version: '4.3.0',
packageName: 'zoroaster',
hasMain: true,
license: 'AGPL-3.0',
bin: {
zoroaster: 'depack/bin/zoroaster.js',
'zoroaster-dev': 'src/bin/index.js'
}
}
When a package exports either a main or a module fields, fpj
will check for their existence to resolve the path to the entry. When the entry does not exist, by default an error will be thrown. To disable the error, and add the entryExists: false
to the output, the Soft Mode can be activated.
/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'
(async () => {
const zoroaster = await fpj(
dirname('example/example.js'),
'myPackage',
{ soft: true },
)
console.log(zoroaster)
})()
{
entry: 'example\\node_modules\\myPackage\\index.js',
packageJson: 'example\\node_modules\\myPackage\\package.json',
version: '1.0.0',
packageName: 'myPackage',
hasMain: true,
entryExists: false
}
© Art Deco™ 2020 |
---|