简体   繁体   English

我可以从 package.json bin 脚本运行命令吗?

[英]Can I run a command from package.json bin script?

Is it possible to run a command from a bin script in a package.json ?是否可以从package.jsonbin脚本运行命令? I know it expects a path to file and trying to run a command there results in an error upon installation (after publishing to npm).我知道它需要一个文件路径并尝试在那里运行命令会导致安装时出错(发布到 npm 后)。 Is it possible to run a command like it is in an npm start ?是否可以像在npm start一样运行命令?

Examples:例子:

{
  "name": "myscript",
  "version": "1.0.3",
  "bin": {
    "myscript": "app/main.js"
  }
}

This will create a symlink from the app/main.js script to /usr/local/bin/myscript这将创建一个从app/main.js脚本到/usr/local/bin/myscript的符号链接
Instead, this is what I want to achieve:相反,这就是我想要实现的目标:

{
  "name": "myscript",
  "version": "1.0.3",
  "bin": {
    "myscript": "echo hello world"
  }
}

Possible workarounds are also appreciated.可能的解决方法也受到赞赏。

Based on what you've written, it seems like you are looking for https://docs.npmjs.com/cli/run-script .根据您所写的内容,您似乎正在寻找https://docs.npmjs.com/cli/run-script

With the package.json script property, you can define scripts as arbitrary shell commands or programs installed into the ./node_modules/.bin .使用package.json脚本属性,您可以将脚本定义为安装到./node_modules/.bin任意 shell 命令或程序。

您是否尝试过运行npm link https://docs.npmjs.com/cli/link这将创建指向您的二进制文件的链接,然后您可以在命令行上以myscript运行您的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在不调用脚本的情况下运行与我的 package.json 脚本相同的命令? - Why I can't run the same command that is my script of package.json without calling the script? 节点,当我运行package.json`bin``mand`时,给我一个'意外令牌附近的语法错误`('` - Node, when I run package.json `bin` `command` , give me `syntax error near unexpected token `(' ` 从命令行运行`node index.js`是可行的,但是当我在package.json中使用启动脚本时,出现了EADDRINUSE / usr / bin / local / node错误 - Running `node index.js` from command line works but when I use the start script in my package.json I get an EADDRINUSE /usr/bin/local/node error 从package.json运行Node.js命令 - Run Node.js command from package.json 是否可以从Node脚本运行package.json脚本? - Is it possible to run package.json scripts from a Node script? `npm run script`使用父目录中的package.json - `npm run script` uses package.json from parent directory npm package.json 定义的 bin 模块 - 找不到命令 - npm package.json defined bin module - command not found 节点 package.json“bin”值,命令不起作用 - Node package.json "bin" value, command not working 如何使用 zx 在 package.json 中运行脚本? - How to run script in the package.json with zx? 运行依赖项 package.json 中的脚本 - Run script that is in package.json of a dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM