Rizwan Saleem
Rizwan Saleem

Reputation: 896

How to change main file in package.json in NodeJs

I am new to Node.js and this is my first project in node. Now I have two main files. For local envoirnment I have index.js file and for production envoirnment production.js. Now I want that when I run command npm start then automaticall file loaded according to envoirnment.

I done following changes in package.json

 "main": "process.env.APP_ENV==undeinfed ? index.js : server.js",
 "scripts": {
   "start": "process.env.APP_ENV==undeinfed ? index.js : server.js",
   "test": "echo \"Error: no test specified\" && exit 1"
  }

When I npm start command then I received following error

[email protected] start /home/php/fayvo/lambda/quick-create-post process.env.APP_ENV==undeinfed ? index.js : server.js

sh: 1: process.env.APP_ENV==undeinfed: not found npm ERR! code ELIFECYCLE npm ERR! syscall spawn npm ERR! file sh npm ERR! errno ENOENT npm ERR! [email protected] start: process.env.APP_ENV==undeinfed ? index.js : server.js npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the [email protected] start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR!
/home/.npm/_logs/2019-12-18T10_11_35_259Z-debug.log

Upvotes: 1

Views: 3753

Answers (1)

Andrei Piatrou
Andrei Piatrou

Reputation: 434

Suggest implementing a one index.js file that does all required branching inside explicitly without hidden logic inside package.json file. It will give much more readable and flexible approach.

Upvotes: 2

Related Questions