OS independent access to variables in package.json

后端 未结 2 1845
面向向阳花
面向向阳花 2020-12-24 06:36

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

\"scripts\": {
    \"preinstall\": \"echo ${npm_packa         


        
相关标签:
2条回答
  • 2020-12-24 07:29

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

    "scripts": {
        "preinstall": "cross-var echo ${npm_package_name}"
    }
    
    0 讨论(0)
  • 2020-12-24 07:29

    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);
    
    0 讨论(0)
提交回复
热议问题