1

Hi I want to switch my app to production mode and I use windows and Visual Studio Code,I use expressjs in backend and creat-react-app in frontend.Below is the data tree enter image description here

Below is the package.json and file where dotenv comes from:

enter image description here

enter image description here And I want to switch it into production mode in backend,below is my backend data tree:

enter image description here

I add script in server.js:

enter image description here

And I also to change NODE_ENV=development into NODE_ENV=production in .envfile. At last I input npm start like below: enter image description here

And I get into localhost:5000,but failed.Anyone could advice?

juexu
  • 131
  • 2
  • 11
  • 1
    where does dotenv come into play? – madflow Sep 29 '21 at 15:15
  • You need to require dotenv in your server.js and load the config: `require("dotenv").config();` (by default, invoking config will load the `.env` at the root directory; if you want to load a specific path see the [dotenv config method documentation](https://github.com/motdotla/dotenv#config)). In addition, you should separate your env files. One for development and one for production, similar to the [CRA recommendation](https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used). – Matt Carlotta Sep 29 '21 at 19:28
  • @MattCarlotta OP already imported it (show in the 3rd image) – justpen Sep 30 '21 at 14:35
  • I add file where dotenv come into play.Even I try built a new `.env.development` file and set `NODE_ENV=production`,it does not work still. – juexu Sep 30 '21 at 14:36
  • Can you try `const result = dotenv.config();` and `console.log(result.error)` to check for errors? – justpen Sep 30 '21 at 14:39
  • See you again thanks.Output is `undefined` – juexu Oct 01 '21 at 16:02
  • @juexu Unable to replicate your issue. Running the `server` script produces the expected [result](https://i.imgur.com/7Ec05i2.png). On a related note, `.env` files shouldn't be commited to git control. To remove it, add it to your `.gitignore` and take a look at this [question](https://stackoverflow.com/questions/1274057/how-can-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitign) to remove it from git cache. – Matt Carlotta Oct 02 '21 at 03:34
  • @MattCarlotta,thanks for your strive.The result I want is that user can get into a e-commence shop main page while user input `http://localhost:5000/` in the browser.But nothing happened now.Thanks your tips on `gitignore`.It would be open until this question closed.Thanks very much. – juexu Oct 04 '21 at 01:23

2 Answers2

0

Maybe you can try to set the path to the .env file explicitly (https://github.com/motdotla/dotenv#path).
Something like .config({ path: path.resolve(__dirname, "../.env") })

justpen
  • 178
  • 1
  • 11
  • Hi @justpen,thanks.I change dotenv.config() into dotenv.config({ path: './.env' }),but still not working. – juexu Oct 01 '21 at 16:23
0

Just removeapp.get('/', (req, res) => { res.send('API is running') }) afterapp.use(express.json())

juexu
  • 131
  • 2
  • 11