5

I am trying to create two separate npm commands to start my NodeJS project locally on the dev machine and in production mode. I want to be able to pass arguments to the machine separately to serve the right dependencies - which could be a CDN in production OR my local machine.

Here's what I am looking to have in package.json

"run": "node ./server/app.js", /* for running locally*/

"start": "node ./server/app.js", /* for running in production*/

If I try to call npm run - it creates this error:

npm ERR! npm run-script [<pkg>] <command>
npm ERR! not ok code 0

I also want to be able to send commandline argumentswhich would contain a URL.

2 Answers 2

5

You are not doing it right.

To call custom scripts, you have to run

npm run-script run

Your package.json should have:

"scripts": {
    "start": "node ./server/app.js",
    "run": "node ./server/app.js"
}

See: https://npmjs.org/doc/cli/npm-run-script.html

Sign up to request clarification or add additional context in comments.

Comments

0

You can use this bash script to run any (even nested) module directly with arguments. Don't forget to change ~/Develop/node_modules to your local path.

#!/bin/bash
if [ $# -eq 0 ]; then
    echo Usage: npmrun module [args]
    exit
fi
data=~/bin/npmrun.data
if [ -f $data ]; then source $data
else declare -A where
fi
cd ~/Develop/node_modules
if [ -z ${where[$1]} ]; then
    path=$(find -path '*/bin/'$1)
    if [ -z $path ]; then
        echo "Module \"$1\" not founded!"
        exit
    else
        where[$1]=$path;
        declare -p where > $data
    fi
fi
${where[$1]} ${@:2}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.