weixin_41568127
?yb?
采纳率100%
2012-07-20 14:02

向 npm 脚本发送命令行参数

The scripts portion of my package.json currently looks like this:

"scripts": {
    "start": "node ./script.js server"
}

...which means I can run npm start to start the server. So far so good.

However, I would like to be able to run something like npm start 8080 and have the argument(s) passed to script.js (e.g. npm start 8080 => node ./script.js server 8080). Is this possible?

转载于:https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

10条回答

  • csdnceshi52 妄徒之命 8年前

    Edit 2014.10.30: It's possible to pass args to npm run as of npm 2.0.0

    The syntax is as follows:

    npm run <command> [-- <args>]

    Note the necessary --. It is needed to separate the params passed to npm command itself and params passed to your script.

    So if you have in package.json

    "scripts": {
        "grunt": "grunt",
        "server": "node server.js"
    }
    

    Then the following commands would be equivalent:

    grunt task:target => npm run grunt -- task:target

    node server.js --port=1337 => npm run server -- --port=1337

    To get the parameter value, see this question. For reading named parameters, it's probably best to use a parsing library like yargs or minimist; nodejs exposes process.argv globally, containing command line parameter values, but this is a low-level API (whitespace-separated array of strings, as provided by the operating system to the node executable).


    Edit 2013.10.03: It's not currently possible directly. But there's a related GitHub issue opened on npm to implement the behavior you're asking for. Seems the consensus is to have this implemented, but it depends on another issue being solved before.


    Original answer: As a some kind of workaround (though not very handy), you can do as follows:

    Say your package name from package.json is myPackage and you have also

    "scripts": {
        "start": "node ./script.js server"
    }
    

    Then add in package.json:

    "config": {
        "myPort": "8080"
    }
    

    And in your script.js:

    // defaulting to 8080 in case if script invoked not via "npm run-script" but directly
    var port = process.env.npm_package_config_myPort || 8080
    

    That way, by default npm start will use 8080. You can however configure it (the value will be stored by npm in its internal storage):

    npm config set myPackage:myPort 9090
    

    Then, when invoking npm start, 9090 will be used (the default from package.json gets overridden).

    点赞 26 评论 复制链接分享
  • weixin_41568184 叼花硬汉 7年前

    You asked to be able to run something like npm start 8080. This is possible without needing to modify script.js or configuration files as follows.

    For example, in your "scripts" JSON value, include--

    "start": "node ./script.js server $PORT"
    

    And then from the command-line:

    $ PORT=8080 npm start
    

    I have confirmed that this works using bash and npm 1.4.23. Note that this work-around does not require GitHub npm issue #3494 to be resolved.

    点赞 30 评论 复制链接分享
  • csdnceshi63 elliott.david 8年前

    From what I see, people use package.json scripts when they would like to run script in simpler way. For example, to use nodemon that installed in local node_modules, we can't call nodemon directly from the cli, but we can call it by using ./node_modules/nodemon/nodemon.js. So, to simplify this long typing, we can put this...

    
        ...
    
        scripts: {
          'start': 'nodemon app.js'
        }
    
        ...
    
    

    ... then call npm start to use 'nodemon' which has app.js as the first argument.

    What I'm trying to say, if you just want to start your server with the node command, I don't think you need to use scripts. Typing npm start or node app.js has the same effort.

    But if you do want to use nodemon, and want to pass a dynamic argument, don't use script either. Try to use symlink instead.

    For example using migration with sequelize. I create a symlink...

    ln -s node_modules/sequelize/bin/sequelize sequelize

    ... And I can pass any arguement when I call it ...

    ./sequlize -h /* show help */
    
    ./sequelize -m /* upgrade migration */
    
    ./sequelize -m -u /* downgrade migration */
    

    etc...

    At this point, using symlink is the best way I could figure out, but I don't really think it's the best practice.

    I also hope for your opinion to my answer.

    点赞 9 评论 复制链接分享
  • csdnceshi65 larry*wei 7年前

    npm 2.x support cli args

    Command

    npm run-script start -- --foo=3

    Package.json

    "start": "node ./index.js"

    Index.js

    console.log('process.argv', process.argv);

    点赞 7 评论 复制链接分享
  • csdnceshi55 ~Onlooker 5年前

    jakub.g's answer is correct, however an example using grunt seems a bit complex.

    So my simpler answer:

    - Sending a command line argument to an npm script

    Syntax for sending command line arguments to an npm script:

    npm run [command] [-- <args>]
    

    Imagine we have an npm start task in our package.json to kick off webpack dev server:

    "scripts": {
      "start": "webpack-dev-server --port 5000"
    },
    

    We run this from the command line with npm start

    Now if we want to pass in a port to the npm script:

    "scripts": {
      "start": "webpack-dev-server --port process.env.port || 8080"
    },
    

    running this and passing the port e.g. 5000 via command line would be as follows:

    npm start --port:5000
    

    - Using package.json config:

    As mentioned by jakub.g, you can alternatively set params in the config of your package.json

    "config": {
      "myPort": "5000"
    }
    
    "scripts": {
      "start": "webpack-dev-server --port process.env.npm_package_config_myPort || 8080"
    },
    

    npm start will use the port specified in your config, or alternatively you can override it

    npm config set myPackage:myPort 3000
    

    - Setting a param in your npm script

    An example of reading a variable set in your npm script. In this example NODE_ENV

    "scripts": {
      "start:prod": "NODE_ENV=prod node server.js",
      "start:dev": "NODE_ENV=dev node server.js"
    },
    

    read NODE_ENV in server.js either prod or dev

    var env = process.env.NODE_ENV || 'prod'
    
    if(env === 'dev'){
        var app = require("./serverDev.js");
    } else {
        var app = require("./serverProd.js");
    }
    
    点赞 4 评论 复制链接分享
  • csdnceshi62 csdnceshi62 8年前

    You could also do that:

    In package.json:

    "scripts": {
        "cool": "./cool.js"
    }
    

    In cool.js:

     console.log({ myVar: process.env.npm_config_myVar });
    

    In CLI:

    npm --myVar=something run-script cool
    

    Should output:

    { myVar: 'something' }
    

    Update: Using npm 3.10.3, it appears that it lowercases the process.env.npm_config_ variables? I'm also using better-npm-run, so I'm not sure if this is vanilla default behavior or not, but this answer is working. Instead of process.env.npm_config_myVar, try process.env.npm_config_myvar

    点赞 2 评论 复制链接分享
  • csdnceshi52 妄徒之命 7年前

    This doesn't really answer your question but you could always use environment variables instead:

    "scripts": {
        "start": "PORT=3000 node server.js"
    }
    

    Then in your server.js file:

    var port = process.env.PORT || 3000;
    
    点赞 2 评论 复制链接分享
  • csdnceshi72 谁还没个明天 4年前

    Use process.argv in your code then just provide a trailing $* to your scripts value entry.

    echoargs.js:

    console.log('arguments: ' + process.argv.slice(2));
    

    package.json:

    "scripts": {
        "start": "node echoargs.js $*"
    }
    

    Examples:

    > npm start 1 2 3
    arguments: 1,2,3
    

    process.argv[0] is the executable (node), process.argv[1] is your script.

    Tested with npm v5.3.0 and node v8.4.0

    点赞 2 评论 复制链接分享
  • csdnceshi62 csdnceshi62 6年前

    If you want to pass arguments to the middle of an npm script, as opposed to just having them appended to the end, then inline environment variables seem to work nicely:

    "scripts": {
      "dev": "BABEL_ARGS=-w npm run build && cd lib/server && nodemon index.js",
      "start": "npm run build && node lib/server/index.js",
      "build": "mkdir -p lib && babel $BABEL_ARGS -s inline --stage 0 src -d lib",
    },
    

    Here, npm run dev passes the -w watch flag to babel, but npm run start just runs a regular build once.

    点赞 1 评论 复制链接分享
  • csdnceshi69 YaoRaoLov 3年前

    I've found this question while I was trying to solve my issue with running sequelize seed:generate cli command:

    node_modules/.bin/sequelize seed:generate --name=user
    

    Let me get to the point. I wanted to have a short script command in my package.json file and to provide --name argument at the same time

    The answer came after some experiments. Here is my command in package.json

    "scripts: {
      "seed:generate":"NODE_ENV=development node_modules/.bin/sequelize seed:generate"
    }
    

    ... and here is and example of running it in terminal to generate a seed file for a user

    > yarn seed:generate --name=user
    
    > npm run seed:generate -- --name=user
    

    FYI

    yarn -v
    1.6.0
    
    npm -v
    5.6.0
    
    点赞 评论 复制链接分享

相关推荐