[英]How can a bash script in an NPM module access its NPM environment variables?
I need to access npm environment variables of a submodule in a bash script that is running within it, when it is called from a parent NPM module.当从父 NPM 模块调用时,我需要访问其中运行的 bash 脚本中子模块的 npm 环境变量。
Project pseudo-structure:项目伪结构:
Parent NPM Project
│
├── node_modules
│ │
│ ├── npm-submodule
│ │ │
│ │ ├── bash-script.sh
│ │ │
│ │ └── package.json
│ .
│
├── package.json
│
.
Parent NPM Project — package.json
:父 NPM 项目 —
package.json
:
{
"name": "parent-project",
"scripts": {
"get-package-name": "echo $npm_package_name",
"get-submodule-name": "npm-submodule"
},
"dependencies": {
"npm-submodule": "1.0.0"
}
}
NPM submodule — package.json
: NPM 子模块 —
package.json
:
{
"name": "npm-submodule",
"bin": "./bash-script.sh",
"scripts": {
"get-package-name": "echo $npm_package_name"
}
}
NPM submodule — bash-script.sh
: NPM 子模块 —
bash-script.sh
:
#!/bin/bash
npm run get-package-name
The Problem问题
From the parent module, when I run npm run get-package-name
, predictably parent-project
is returned.在父模块中,当我运行
npm run get-package-name
时,可以预见会返回parent-project
。
When I run npm run get-submodule-name
, nothing is returned — it looks like the npm environment variables for the submodule aren't populated within the submodule when it is being called in this way.当我运行
npm run get-submodule-name
时,没有返回任何内容——当以这种方式调用时,子模块的 npm 环境变量似乎没有填充到子模块中。
In a bash-based context , how can a submodule access it's own NPM environment values, or failing that, read them directly from package.json
?在基于bash 的上下文中,子模块如何访问它自己的 NPM 环境值,或者如果失败,直接从
package.json
读取它们?
A solution that I came up with that works is to grab the folder that the bash script is in, and do this:我提出的一个可行的解决方案是获取 bash 脚本所在的文件夹,然后执行以下操作:
#!/bin/bash
MODULE_DIR=$(dirname "$(realpath "$0")")
cd "$MODULE_DIR" && npm run get-package-name -s
I tested it with npm link
and it works globally within any other NPM project, but am still interested in other solutions.我用
npm link
测试了它,它在任何其他 NPM 项目中全局工作,但我仍然对其他解决方案感兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.