1

As I have different channel to run my code in npm command.

In the npm scripts, I will write

"scripts": {
  "start": "webpack-dev-server",
  "start:channle_1": "CHANNEL=channe_1 webpack-dev-server",
  "start:channle_2": "CHANNEL=channe_2 webpack-dev-server"
}

And in webpack.config.js, I can define variable to judge I'm in which channel.

  const CHANNEL = process.env.CHANNEL || 'channe_1';

  console.log('===', process.env.CHANNEL);

  webpackConfig.plugins.push(
    new webpack.DefinePlugin({
      CHANNEL: JSON.stringify(CHANNEL),
    })
  );

So, in my code, there is a glable CHANNEL I can use.

But, there is a problem in my npm scripts. I want separate sever and channel.I hope it can like this

"scripts": {
  "start": "webpack-dev-server",
  "start:channle_1": "CHANNEL=channe_1 & npm run start",
  "start:channle_2": "CHANNEL=channe_2 & npm run start"
}

As I know, & can let npm command run currently, but, in my situation, the variable CHANNEL can't pass in process.env.CHANNEL.

So, how can I solve this problem?

1 Answer 1

2

If you take out the & it should work fine:

"start:channle_1": "CHANNEL=channe_1 npm run start"

When you put & in there, it's going to try to run the preceding (empty) command in the background. You can set environment variables by prefixing them from the command, separated by whitespace.

Sign up to request clarification or add additional context in comments.

1 Comment

Should also add that if you want your script to work on windows, you will have to use github.com/kentcdodds/cross-env.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.