The `postinstall` script helper for handling native bindings in legacy `npm` versions
npm install napi-postinstall










The postinstall script helper for handling native bindings in legacy npm versions, this is a reimplementation of the [node-install][node-install] functionality from [esbuild][esbuild] for [napi-rs][napi-rs] ecosystem packages like [rollup][rollup], [@swc/core][swc-core] and [unrs-resolver][unrs-resolver].
For more details, please refer to the following issues:
- npm/cli#4828 -- root cause
- napi-rs/napi-rs#2569
- unrs/unrs-resolver#56
- Usage
- Install
- CLI
- API
- Types
- Example
- Sponsors and Backers
- Sponsors
- Backers
- Changelog
- License
``shpnpm
pnpm add napi-postinstall
$3
`sh
napi-postinstall unrs-resolver #
`You can put it into
scripts#postinstall of your package.json:`json
{
"scripts": {
"postinstall": "napi-postinstall unrs-resolver"
}
}
`This will check and prepare the napi binding packages for you automatically.
$3
#### Types
`ts
// napi-postinstall
export interface PackageJson {
name: string
version: string
}export declare function checkAndPreparePackage(
packageNameOrPackageJson: PackageJson | string,
checkVersion?: boolean,
): Promise
// napi-postinstall/fallback
declare function fallback(
packageJsonPath: string,
checkVersion?: boolean,
): T
export = fallback
`#### Example
`js
// index.js
const { checkAndPreparePackage, isNpm } = require('napi-postinstall')if (isNpm()) {
void checkAndPreparePackage('unrs-resolver' / /)
}
// fallback.js
module.exports = require('napi-postinstall/fallback')(
require.resolve('./package.json') / /,
true / /,
)
``
| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|  |  |  |
| 1stG | RxTS | UnTS |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
|  |  |  |
Detailed changes for each release are documented in CHANGELOG.md.
[MIT][] © [JounQin][]@[1stG.me][]
[node-install]: https://github.com/evanw/esbuild/blob/4475787eef4c4923b92b9fa37ebba1c88b9e1d9b/lib/npm/node-install.ts
[esbuild]: https://github.com/evanw/esbuild
[napi-rs]: https://github.com/napi-rs/napi-rs
[rollup]: https://github.com/rollup/rollup
[swc-core]: https://github.com/swc-project/swc
[unrs-resolver]: https://github.com/unrs/unrs-resolver
[1stG.me]: https://www.1stG.me
[JounQin]: https://github.com/JounQin
[MIT]: http://opensource.org/licenses/MIT