@@ -27,6 +27,7 @@ npm install postinstall-build --save
2727- [ Caveats] ( #caveats )
2828 - [ Bugs in Yarn] ( #bugs-in-yarn )
2929 - [ Bugs in npm] ( #bugs-in-npm )
30+ - [ Building a file referenced by package.json ` bin ` ] ( #building-a-file-referenced-by-packagejson-bin )
3031
3132<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3233
@@ -259,3 +260,32 @@ my knowledge they are no fault of this package and are widely reported npm bugs.
259260 npm has trouble making lots of connections to its own registry. You can use
260261 ` npm config set fetch-retries 5 ` (for example) to work around this; using the
261262 non-HTTPS registry might also help.
263+
264+ ### Building a file referenced by package.json ` bin `
265+
266+ If your ` package.json ` file uses the ` bin ` field, and that file does not exist
267+ before building, you may see an error like this:
268+
269+ ``` console
270+ ENOENT: no such file or directory, chmod '[…]/lib/index.js'
271+ ```
272+
273+ This happens because npm needs to symlink any files referenced in the ` bin `
274+ field and make them executable, but this step is performed before ` postinstall ` .
275+ ` postinstall-build ` can’t do anything to address this shortcoming, but the
276+ solution is simple. Create a simple non-built file (that is, not created during
277+ the build step) that imports the built file you actually want to target. For
278+ example, you could create a top-level file called ` cli.js ` like so:
279+
280+ ``` js
281+ require (' ./lib/index' );
282+ ```
283+
284+ Or, export the program’s behavior in a function and call it:
285+
286+ ``` js
287+ require (' ./lib/index' ).run ();
288+ ```
289+
290+ Make sure to update your ` bin ` field to the new file (in this case, ` cli.js ` )
291+ and include it in your npm package and repository.
0 commit comments