Using an if statement to run a script in Npm

Is there a way to configure an npm run script to use different AWS accounts based on the environment?

{
  "config": {
    "acc": if ({npm_config_env} == "dev") "account1" else "account_2" 
  },
   "scripts":{
     "con": "AWS_DEFAULT_PROFILE=${npm_package_config_acc} aws s3"
   }
}

For example, when running npm run con --env=dev, account1 should be used.

And when running npm run con --env=prod, account2 should be used.

I've searched online but couldn't find any information on whether or not npm run can handle if statements. If anyone knows of a workaround, I would greatly appreciate it!

Answer №1

Since package.json is in JSON format, it does not support if else or any other programming syntax.

An alternative solution for this scenario is to use if else statements from a shell script, as the scripts in package.json are executed in a shell environment.

{
  "scripts": {
    "con": "AWS_DEFAULT_PROFILE=$(if [ $npm_config_env = dev ]; then echo \"account1\"; else echo \"account_2\"; fi) aws s3",
  }
}

The provided script will assign a value to AWS_DEFAULT_PROFILE based on certain conditions.

Important Note: Since these scripts run in a shell, they should be specific to the type of shell being used. The above scripts are designed to work in bash or similar shells.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach for managing multiple socket rooms?

I have been working on developing a chat application that supports multiple chat rooms. After watching several tutorials, I have devised a way to handle this. const io = require("socket.io")(http); io.on("connection", (socket) => { ...

Removing a Dependency/Plugin in Laravel: A Step-by-Step Guide

My package.json file includes the following dependencies: "dependencies": { "moment": "^2.24.0", "progressbar": "^1.3.0", "vue-progressbar": "^0.7.5", "vue-router": "^3.1.5" } I am looking to remove "progressbar": "^1. ...

VueJS with Vuetify: Issue with draggable cards in a responsive grid

I am currently working on creating a gallery that allows users to rearrange images. To test this functionality, I am using an array of numbers. It is important that the gallery is responsive and displays as a single column on mobile devices. The issue I ...

The Angular User Interface Router is a versatile tool for managing

Managing a large application with over 1500 pages can be challenging. I have opted to use angular UI Router for routing. I am interested in learning about the best practices for handling routing in such a vast application. Should I define all routes in ...

Use jQuery in MVC 5 to remove each cloned div individually starting from the last one, excluding the default div

My default setting is as shown below. https://i.stack.imgur.com/m8Hpr.png The add button functions correctly, but I am having trouble removing cloned divs. I can only remove the last one, instead of each cloned div individually starting from the last, exc ...

"Patience is key when waiting for an AJAX response within a jQuery loop

I've been facing difficulties in making this work using the $.Deferred object. Here is a simplified version of the code setup. g_plans = []; $(function(){ // Need to utilize a custom ajax function that returns a promise object var pageLoadPro ...

issue with node script not finishing

I've encountered an issue with my data dump script being used alongside an npm script. The problem arises when I try to dump data into the database before starting my node application server. Unfortunately, the dump script never completes, causing the ...

"Assure that setTimeout will be executed within the then method in

Within an Immediately Invoked Function Expression (IFFE), I am returning a Promise. The first thing that is logged is first, followed by third, and then second. Despite having the setTimeout inside the then block, shouldn't everything within it be exe ...

Creating smooth animations in JavaScript without relying on external libraries such as jQuery

Is there a way in JavaScript to make a <div> slide in from the right when hovered over, without relying on jQuery or other libraries? I'm looking for a modern browser-friendly solution. const div = document.querySelector('.fro ...

Setting a callback function as a prop for react-paginate in TypeScript: A step-by-step guide

When using react-paginate, there is a prop called onPageChange with the following type: onPageChange?(selectedItem: { selected: number }): void; After implementing it like this: const onPageChange = (selected): void => { console.log(selected); } ...

Unlawful use of the return statement

Can you identify the issue with this code? The browser reports: "Uncaught SyntaxError: Illegal return statement" I'm looking for an explanation in this format: 1 2 3fool 4 5bar 6fool 7 8 9bar... let arr = []; for (i = 0; i <= 100; i++) { if ( ...

Clicking an AngularJS button within a form is causing an unexpected page refresh

I am facing an issue with adding a new input field on click. I have two buttons, one to add and another to remove the input box. When I click on the add button, it should add a set of input boxes, but currently, it only adds one and refreshes the page, dis ...

Ways to troubleshoot the browserify error below

Exploring the world of gulp and browserify is a new adventure for me. I'm currently following an online tutorial that involves the usage of browserify and gulp. Here's a glimpse at my package.json: { "name": "progressive", "version": "1.0.0 ...

VueJS added a new object to the array, but the data did not update reactively

Here is my current data layout days: [ { id: 0 name: 'Monday', times: [] }, { id: 1 name: 'Tuesday', times: [] } } I have implemented the following function to append an object to the times array. onT ...

The React server-side rendering isn't reflecting changes made on the client-side route

Upon the first refresh, both the server and client side are updated; however, subsequent updates only affect the client side when switching pages with react router. For instance, refreshing the page or entering a new URL causes changes on the server and t ...

Get rid of numerous div elements in a React.js application with the help of a Remove button

I need help figuring out how to efficiently remove the multiple div's that are generated using the add button. I am struggling to grasp how to pass the parent div's id into the delete method from the child div. Additionally, I'm wondering if ...

Updating the URL in React and Redux

I am currently working on developing an application using React and Redux (DVA) with Ant.Design as the main framework. I am facing an issue where I need to change the URL when a user clicks on a button, and connect that URL change to a specific action so t ...

Elements from Firebase failing to appear in Angular Grid

I'm struggling to populate items within this grid sourced from Firebase. I was able to make it work with a custom Service that returned a fixed array. I can confirm that the data is being received by the browser as I can log it out in JSON format lik ...

When using Node.js with Express and ssh2, my data structures remain intact without any resets when loading web pages

To display jobs sent by users to a cluster, the code below is used (simplified): var split = require('split'); var Client = require('ssh2').Client; var conn = new Client(); var globalRes; var table = [["Head_1","Head_2"]]; module.exp ...

Enhancing the Efficiency of JavaScript's indexOf Method

I am currently developing a basic search algorithm in JavaScript. var title = "Discovering the Best Book of All Time"; var search1 = "of DiscoverinG boOk Best"; var search2 = "Of TIme best all" var search3 = "Book discovering time" When using indexOf(), ...