How to set shell for npm run-scripts in Windows

后端 未结 7 2034
执笔经年
执笔经年 2020-11-28 02:46

I\'m running npm on Windows and would like to use & style parallel operations in run-scripts but running in parallel in cmd is kind of messy in my package.json file I\'

相关标签:
7条回答
  • 2020-11-28 02:49

    Here's one way to do it:

    1. Create a script, such as my_script.sh, in your project bin directory.
    2. In your package.json file, add a line to run the script using bash. For example:

      "scripts": {
        "boogie": "bash bin/my_script.sh"
      }
      

    Now you can run your bash script from npm by:

        npm run-script boogie
    

    Not very elegant, but it works.

    If you are developing in both Windows and Linux/Unix, then at least this approach is fairly portable to both environments.

    0 讨论(0)
  • 2020-11-28 02:50

    In my case I just needed to run npm start from inside Bash. I run cmd then I open bash by running "c:\Program Files\Git\bin\bash.exe". Under bash shell I then was able to call npm build and npm start succesfully.

    You may already have bash if you are using Git. If not, you can install it.

    Hope this may save someone's time.

    0 讨论(0)
  • just using CMD's way to run .bat!

    .json
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject",
        "app": "cd build & browser-sync start --server --files 'index.html'",
        "bat": "start start-browser.bat",
        "starts": "start http://localhost:7777/datas/ && start http://localhost:7777/Info/"
    },
    
    .bat
    start http://localhost:7777/datas/ && start http://localhost:7777/Info/
    
    0 讨论(0)
  • 2020-11-28 02:58

    Since npm 5.1

    npm config set script-shell "C:\\Program Files (x86)\\git\\bin\\bash.exe"  
    

    or (64bit installation)

    npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
    

    Note that you need to have git for windows installed.

    You can revert it by running:

    npm config delete script-shell
    
    0 讨论(0)
  • 2020-11-28 03:03

    Use a specifically created node_module for this purpose. I suggest using npm-run-all, but others exists, such as parallelshell.

    Parallelshell example is below for drop-in-replacement for your question.

    "scripts": {
        "parallelexample1": "parallelshell \"echo 1\" \"echo 2\" \"echo 3\""
    },
    

    following command:

    npm run parallelexample1
    

    works both on windows and unix(Linux/MacOS).

    Interestingly npm-run-all does not support shell commands; therefore we need to put all shell commands to separate scripts like below.

    "scripts": {
       "parallelexample2": "npm-run-all echo*",
        "echo1": "echo 1",
        "echo2": "echo 2",
        "echo3": "echo 3"
    },
    

    Following command:

    npm run parallelexample2
    

    works both on windows and unix(Linux/MacOS).

    0 讨论(0)
  • 2020-11-28 03:04

    You can also use cross-platform powershell https://github.com/powershell/powershell#get-powershell for npm scripts.

    To set for single project, run this from project root folder:

    npm config set script-shell pwsh --userconfig ./.npmrc
    

    To globally set for all node projects:

    npm config set script-shell pwsh [--global]
    
    0 讨论(0)
提交回复
热议问题