1

I have a .js file in my project that imports an NPM package and has 2 functions that use that package:

//replacePaths.js
import {replaceInFile} from 'replace-in-file';
async function replace() {
    const options = {
        files: 'src/**/*.jsx',
        from: 'dev/svgs/icons-main.svg',
        to: 'staging/media/svgs/icons-main.svg',
    };

    try {
        const results = await replaceInFile(options);
    } catch (error) {
        console.error('Error occurred:', error);
    }
}

async function revert() {
    const options = {
        files: 'src/**/*.jsx',
        from: 'staging/media/svgs/icons-main.svg',
        to: 'dev/svgs/icons-main.svg',
    };

    try {
        const results = await replaceInFile(options);
    } catch (error) {
        console.error('Error occurred:', error);
    }
}
module.exports = {
    replace,
    revert
};

I want to be able to run these functions from the command line. I've Googled around and to do this it seems I just add the following to my package.json file:

"scripts": {
    ...
    "replace": "node replacePaths.js replace",
    "revert" : "node replacePaths.js revert",
    ...
}

I the first of these in the command line as such: npm run replace, but I got the error ReferenceError: module is not defined in ES module scope.

Googling that, I changed the exports part to:

export default replace;
export default revert;

and tried again. I didn't get any errors, but instead just got the line node replacePaths.js replace printed in the console.

I can run the functions by removing the export part and just having replace() or revert() on the JS file, but that's not helpful to me if I want to have 2 functions on the same file.

Would anyone be able to point me in the right direction?

6
  • no point exporting in that file, since nothing is importing it. you don't call your functions anywhere - you'll need to use process.argv to get the command line arguments, then some logic to call the appropriate function Commented Aug 13, 2024 at 4:03
  • @JaromandaX ok, but how do I run those functions from the command line?
    – MeltingDog
    Commented Aug 13, 2024 at 4:07
  • you'll need to use process.argv to get the command line arguments, then some logic to call the appropriate function Commented Aug 13, 2024 at 4:14
  • 1
    await ({ replace, revert })[process.argv.at(-1)]?.(); at the end of the file will do it Commented Aug 13, 2024 at 4:20
  • Thanks. Want to make that an answer so I can accept?
    – MeltingDog
    Commented Aug 13, 2024 at 4:28

3 Answers 3

2

here's the simplest answer - to run the function named on the command line

import {replaceInFile} from 'replace-in-file';
async function replace() {
    // snip
}

async function revert() {
    // snip
}
// add this to actually run a function
await ({ replace, revert })[process.argv.at(-1)]?.();
1

To run a different command depending on a command-line argument, your top-level code could be structured something like this:

const [,,command] = process.argv;

try {
  if (command === 'replace') {
      await replace();
  } else if (command === 'revert') {
      await revert();
  } else {
      console.error('Unknown command. Use "replace" or "revert".');
  }
} catch (e) {
  console.error(e);
}

Examples of running it:

node replacePaths.js replace
node replacePaths.js revert
1
// file: app.js
const functionOne = () => {
    console.log("Function One Executed");
};

const functionTwo = () => {
    console.log("Function Two Executed");
};

const args = process.argv.slice(2);

if (args[0] === 'functionOne') {
    functionOne();
} else if (args[0] === 'functionTwo') {
    functionTwo();
}
1
  • Your answer could be improved by adding more information on what the code does and how it helps the OP.
    – Tyler2P
    Commented Aug 18, 2024 at 20:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.