OS independent access to variables in package.json

荒凉一梦 提交于 2019-11-28 21:03:05

问题


To access a variable in npm scripts you would do something like this in your package.json:

"scripts": {
    "preinstall": "echo ${npm_package_name}"
}

The problem is that works only in Unix, not Windows, where you have to use %npm_package_name%.

Is there a way to do this OS independent? It will be good if npm could do such a variable expansion, before invoking the command.


回答1:


To make it cross-platform, use cross-var:

"scripts": {
    "preinstall": "cross-var echo ${npm_package_name}"
}



回答2:


There's no known way to do this that's OS independent.

A good workaround is to execute the command within a node script:

First, change the preinstall command to execute a node script:

"scripts": {
    "preinstall": "node nameEcho.js"
}

Then you define the command in the nameEcho.js file:

// require the package.json file
var pjson = require('./package.json');

// echo the package's name
console.log(pjson.name);


来源:https://stackoverflow.com/questions/36260436/os-independent-access-to-variables-in-package-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!