Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

如何在 package.json scripts 里使用 .env 的环境变量?

有多个fork的项目,只是 --port 不一样,如:

  "scripts": {
    // 项目1
    "android": "react-native run-android --port=8081", 
    // 项目2
    // "android": "react-native run-android --port=8082",
    // 项目3
    // "android": "react-native run-android --port=8083",
    // ....
  },

我希望把 --port 抽离出来,结果如下:

  "scripts": {
    "android": "react-native run-android --port=${port}", 
  },

项目1:

// .env
port = 8081

项目2:

// .env
port = 8082

项目3:

// .env
port = 8083

请问如何做到这种效果呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

在代码里面获取script里的参数倒是可以,反过来没试过。看题主想要解决什么问题,如果只是想保证端口不冲突,可以使用portfinder之类的库,检查端口是否被占有,动态获取端口


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...