xgqfrms / learning

learning : A collection of all kinds of resources, videos, pdf, blogs, codes... 📚 + 💻 + ❤
https://learning.xgqfrms.xyz
MIT License
16 stars 12 forks source link

how to use npm run bin script form the local package.json file #146

Open xgqfrms opened 11 months ago

xgqfrms commented 11 months ago

how to use npm run bin script form the local package.json file

目的,用于本地脚本测试

{
  "name": "nodejs_playground",
  "version": "0.0.1",
  "description": "Node.js v20.6.x+",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "$npm_package_bin_npcli",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "npcli": "node --env-file=config.env index.js"
  },
  "keywords": [
    "Node.js"
  ],
  "author": "xgqfrms",
  "license": "MIT"
}

image

等价于

# Node.js v20.6.x+ ✅
$ node --env-file=config.env index.js

refs

https://github.com/npm/npm/issues/7137#issuecomment-361069180

https://www.cnblogs.com/xgqfrms/p/17693872.html#5216528

xgqfrms commented 11 months ago
#!/usr/bin/env bash

# node -v
node --env-file=config.env index.js

image

{
  "name": "nodejs_playground",
  "version": "0.0.1",
  "description": "Node.js v20.6.x+",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "$npm_package_bin_npcli_old",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "npcli_old": "node --env-file=config.env index.js",
    "npcli_sh": "./npcli.sh",
    "npcli": "./npcli"
  },
  "keywords": [
    "Node.js"
  ],
  "author": "xgqfrms",
  "license": "MIT"
}

https://stackoverflow.com/questions/77244252/how-can-i-load-a-env-file-in-a-nodejs-cli-application-in-the-package-json-bin

xgqfrms commented 11 months ago

https://github.com/xgqfrms/Raspberry-Pi/tree/master/Pi-4B/node-playground

xgqfrms commented 11 months ago

https://2ality.com/2022/08/installing-nodejs-bin-scripts.html https://2ality.com/2016/01/locally-installed-npm-executables.html

xgqfrms commented 11 months ago

npx

eric@rpi4b:~/Desktop/node-playground $ ane PORT=8888
-bash: ane:未找到命令
eric@rpi4b:~/Desktop/node-playground $ npm-do ane PORT=8888
-bash: npm-do:未找到命令
eric@rpi4b:~/Desktop/node-playground $ npx ane PORT=8888

🚀 your node.js version = 18

🎮 creating...

🎉 finished!
eric@rpi4b:~/Desktop/node-playground $ ./node_modules/app-node-env/ane PORT=8888

🚀 your node.js version = 18

🎮 creating...

🎉 finished!
eric@rpi4b:~/Desktop/node-playground $

npx

http://disq.us/p/2w3phvp

https://2ality.com/2016/01/locally-installed-npm-executables.html