Directly call globally installed Node.js modules

前端 未结 1 407
盖世英雄少女心
盖世英雄少女心 2021-01-13 06:23

Supposed I want to write a module for Node.js that shall be installed globally. I do not want to write any C++ (or something else), but plain Node.js code.

Basically

相关标签:
1条回答
  • 2021-01-13 06:59

    You need to write an executable file. This is what will be executed when a user types your command. Here's an example taken from JSHint:

    #!/usr/bin/env node
    
    require("./../src/cli/cli.js").interpret(process.argv);
    

    Convention says to place this file in a bin directory in the root of your project. You then just need to update your package.json file to tell it where to find your executable:

    {
        "bin": {
            "jshint": "./bin/jshint"
        }
    }
    

    In this case, the executable file can be run from the terminal with the jshint command. When it runs, it simply requires another file and calls a method in it, passing through any command line arguments.

    0 讨论(0)
提交回复
热议问题