How do I add a custom script to my package.json file that runs a javascript file?

前端 未结 6 875
生来不讨喜
生来不讨喜 2020-11-29 17:01

I want to be able to execute the command script1 in a project directory that will run node script1.js.

script1.js is a file in

6条回答
  •  爱一瞬间的悲伤
    2020-11-29 17:19

    Steps are below:

    1. In package.json add:

      "bin":{
          "script1": "bin/script1.js" 
      }
      
    2. Create a bin folder in the project directory and add file runScript1.js with the code:

      #! /usr/bin/env node
      var shell = require("shelljs");
      shell.exec("node step1script.js");
      
    3. Run npm install shelljs in terminal

    4. Run npm link in terminal

    5. From terminal you can now run script1 which will run node script1.js

    Reference: http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

提交回复
热议问题