简体   繁体   中英

What is the correct way to use a package on different environment?

I have 3 different environment: development, staging and production

In each of those environnement, I have a nodeJS application that uses a package that I created. The package is hosted in a private npm registry that I self-host.

I'd like the development application to use to development version of my package, the staging application to use the staging version of my package, and so on...

At first I wanted to create 3 version of my package ( my-package_development , my-package_staging , etc) but it was not maintainable

What is the correct way to do that?

You can use any of below approach


Approach 1

Use environment variable inside your package:

switch (process.env.NODE_ENV){
        case "development":
          //enter code here
          break;
        case "staging":
          //enter code here
          break;
        default:
          //enter code here
}

run your code like this

NODE_ENV=development node server.js

Approach 2

Pass the environment directly to your package

const package_name = require("your_package")(process.env.NODE_ENV)

and then use the switch case to execute the code accordingly

switch (env){
            case "development":
              //enter code here
              break;
            case "staging":
              //enter code here
              break;
            default:
              //enter code here
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM