How to use executables from a package installed locally in node_modules?

前端 未结 22 2141
时光说笑
时光说笑 2020-11-22 12:40

How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:

npm install coffee-script
22条回答
  •  不知归路
    2020-11-22 12:58

    I encountered the same problem and I don't particularly like using aliases (as regular's suggested), and if you don't like them too then here's another workaround that I use, you first have to create a tiny executable bash script, say setenv.sh:

    #!/bin/sh
    
    # Add your local node_modules bin to the path
    export PATH="$(npm bin):$PATH"
    
    # execute the rest of the command
    exec "$@"
    

    and then you can then use any executables in your local /bin using this command:

    ./setenv.sh 
    ./setenv.sh 6to5-node server.js
    ./setenv.sh grunt
    

    If you're using scripts in package.json then:

    ...,
    scripts: {
        'start': './setenv.sh '
    }
    

提交回复
热议问题