简体   繁体   English

如何在 NodeJs 中更改 package.json 中的主文件

[英]How to change main file in package.json in NodeJs

I am new to Node.js and this is my first project in node.我是 Node.js 的新手,这是我在 node.js 中的第一个项目。 Now I have two main files.现在我有两个主要文件。 For local envoirnment I have index.js file and for production envoirnment production.js.对于本地环境,我有 index.js 文件和生产环境 production.js。 Now I want that when I run command npm start then automaticall file loaded according to envoirnment.现在我希望当我运行命令 npm start 然后根据环境加载自动所有文件。

I done following changes in package.json我在 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当我 npm start 命令时,我收到以下错误

quick-create-post@1.0.0 start /home/php/fayvo/lambda/quick-create-post process.env.APP_ENV==undeinfed ? quick-create-post@1.0.0 start /home/php/fayvo/lambda/quick-create-post process.env.APP_ENV==undeinfed ? index.js : server.js index.js : server.js

sh: 1: process.env.APP_ENV==undeinfed: not found npm ERR! sh: 1: process.env.APP_ENV==undeinfed: not found npm ERR! code ELIFECYCLE npm ERR!代码 ELIFECYCLE npm ERR! syscall spawn npm ERR!系统调用产生 npm ERR! file sh npm ERR!文件 sh npm 错误! errno ENOENT npm ERR! errno ENOENT npm 错误! quick-create-post@1.0.0 start: process.env.APP_ENV==undeinfed ? index.js : server.js quick-create-post@1.0.0 开始: process.env.APP_ENV==undeinfed ? index.js : server.js process.env.APP_ENV==undeinfed ? index.js : server.js npm ERR! process.env.APP_ENV==undeinfed ? index.js : server.js npm ERR! spawn ENOENT npm ERR!生成 ENOENT npm ERR! npm ERR! npm 错误! Failed at the quick-create-post@1.0.0 start script. quick-create-post@1.0.0 启动脚本失败。 npm ERR! npm 错误! This is probably not a problem with npm.这可能不是 npm 的问题。 There is likely additional logging output above.上面可能有额外的日志输出。

npm ERR! npm 错误! A complete log of this run can be found in: npm ERR!可以在以下位置找到此运行的完整日志:npm ERR!
/home/.npm/_logs/2019-12-18T10_11_35_259Z-debug.log /home/.npm/_logs/2019-12-18T10_11_35_259Z-debug.log

Suggest implementing a one index.js file that does all required branching inside explicitly without hidden logic inside package.json file.建议实现一个index.js文件,该文件在package.json文件中明确执行所有必需的分支,而无需隐藏逻辑。 It will give much more readable and flexible approach.它将提供更具可读性和灵活性的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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