Issue
Based on:
https://docs.npmjs.com/cli/v10/configuring-npm/package-json#bin
I am trying to create a "binary" for my npm package. My package.json
looks like this:
{
"name": "@internal/my-exe",
"version": "0.0.0",
"type": "commonjs",
"bin": {
"my-exe": "./dist/src/index.js"
},
"files": [
"dist",
"README.md"
],
"scripts": {
"build": "tsc --project tsconfig.build.json",
"ts-node": "ts-node"
},
"dependencies": {
"tslib": "^2.3.0"
},
"devDependencies": {
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"engines": {
"node": ">=18"
},
"volta": {
"node": "18.16.1"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
}
}
and the project (including build output/dist):
my-exe
dist/
-> src/
-> index.js
-> index.d.ts
src/
-> index.ts
package.json
tsconfig.json
tsconfig.build.json
And src/index.ts:
#!/usr/bin/env node
// eslint-disable-next-line node/shebang
export * from './src/index';
I build and pack the above locally with:
npm run build
npm pack
so I now have:
my-exe
dist/
...
internal-my-exe-0.0.0.tgz
package.json
In another test project I then test that I can install/run the above 'binary':
{
"name": "test-app",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18"
},
"volta": {
"node": "18.16.1"
},
"dependencies": {
"@internal/my-exe": "file:../../../my-exe/internal-my-exe-0.0.0.tgz"
}
}
with:
cd test-app
npm i
And now have:
test-app
node_modules/
-> .bin/
-> my-exe
so that looks great. But when I try to run it:
cd test-app
npm my-exe
I just get:
Unknown command: "my-exe"
To see a list of supported npm commands, run:
npm help
If I do:
cat test-app/node_modules/.bin/my-exe
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./src/index"), exports);
So it seems it removed the #!/usr/bin/env node
when installing it but not sure if that the issue.
UPDATE: The above issue was a npm cache "issue" so removing the cache in ~/.npm and deleting the package lock file fixed that when testing locally using npm pack (alternatively update the version each time).
What am I missing?
Solution
You would use the npm exec
command to execute executables located in node_modules/.bin
:
npm exec my-exe
Explanation
When you use npm my-exe
, npm assumes that you're trying to execute a sub-command that's named my-exe
rather than an executable with the same name, hence it shows this warning:
Unknown command: "my-exe"
To see a list of supported npm commands, run:
npm help
Answered By - Edric
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.