npm - How to set max-old-space-size in npm - node.js

I am getting the below error while building the angular project,
CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
I have seen somewhere that configuring max-old-space-size in npm will solve the issue. But I don't know how to set it. I am using Windows OS & there are two npm files inside the nodejs installation folder (C:\Program Files\nodejs), npm & npm.cmd. First line inside the npm.cmd file is "Created by npm, please don't edit manually". So I am not supposed to edit this file & I have to configure max-old-space-size inside npm file. But I don't where in this file I have to configure it. My npm file looks like below,
#!/bin/sh
(set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix
basedir=`dirname "$0"`
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
NODE_EXE="$basedir/node.exe"
if ! [ -x "$NODE_EXE" ]; then
NODE_EXE=node
fi
NPM_CLI_JS="$basedir/node_modules/npm/bin/npm-cli.js"
case `uname` in
*MINGW*)
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
NPM_PREFIX_NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js"
if [ -f "$NPM_PREFIX_NPM_CLI_JS" ]; then
NPM_CLI_JS="$NPM_PREFIX_NPM_CLI_JS"
fi
;;
*CYGWIN*)
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
NPM_PREFIX_NPM_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npm-cli.js"
if [ -f "$NPM_PREFIX_NPM_CLI_JS" ]; then
NPM_CLI_JS="$NPM_PREFIX_NPM_CLI_JS"
fi
;;
esac
"$NODE_EXE" "$NPM_CLI_JS" "$#"

Possibly you could try this,
> npm run start --node-flags --max-old-space-size=512 --no-warnings
> node --max-old-space-size=250 `which npm` some_npm_command

I faced similar issue but i had to increase docker memory size.

Related

Problems creating a React-node app with npm

The instructions are as following:
You are about to start creating a React-node app. Create the file
package.json using npm commands.
Make use of the following information:
The name of the app should be npm_package. The start point will be
index.js The project should have the following elements of
dependencies
- Install the latest version of react
- Lodash with major version 4 and minor version 17,
- Redux with Major version 4,
- Mocha for testing in Dev,
- Eslint with major version 6 in Dev.
Here is my virtual environment for reference:
Virtual Environment
I tried the following commands:
npm init -y
npm install react --save
npm install lodash#4.17.0 --save
npm install redux#4.0.0 --save
npm install mocha --save-dev
npm install eslint#6.0.0 --save-dev
Correctness is determined by the test file score.sh:
#!/bin/sh
PASS=0
FAIL=0
TEST_1=$(grep -o -e "\"dependencies\"" -e "react" -e "\"redux\":\s*\"\^4.*\"" -e "\"lodash\":\s*\"\^4.17.*\"" /projects/challenge/package.json| wc -l)
TEST_2=$(grep -o -e "\"devDependencies\":\s*{" -e "\"eslint\":\s*\"^6.*\"" -e "\"mocha\":\s*" /projects/challenge/package.json| wc -l)
TEST_3=$(find /projects/challenge/node_modules | wc -l)
TEST_4=$(grep -o -e "\"name\":\s*\"npm_package\"" /projects/challenge/package.json| wc -l)
if [ "$TEST_1" -eq 4 ]
then ((PASS++))
fi;
if [ "$TEST_2" -eq 3 ]
then ((PASS++))
fi;
if [ "$TEST_3" -ge 1 ]
then ((PASS++))
fi;
if [ "$TEST_4" -eq 1 ]
then ((PASS++))
fi;
FAIL=$(( 4 - $PASS ))
echo "Test cases executed = 4";
echo "PASS = $PASS FAIL=$FAIL"
Here is my console when I run the tests:
user#workspace5zx0357qxb7p4nvt:/projects/challenge$ bash score.sh Test
cases executed = 4 PASS = 3 FAIL=1
user#workspace5zx0357qxb7p4nvt:/projects/challenge$
It does not tell me which test failed (or much of any other information) and therefore I dont know which of my commands I inputted is wrong or am I missing commands
Since no one has decided to reply, let me.
Just go into package.json and change the name to npm_package.

What do the files in bin folder actually used for?

I am currently working on a node project that requires eslint as a dependency. Hence, a file named eslint and eslint.cmd was created automatically in the root_directory/node_modules/.bin folder.
The content of the eslint file is -
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../eslint/bin/eslint.js" "$#"
ret=$?
else
node "$basedir/../eslint/bin/eslint.js" "$#"
ret=$?
fi
exit $ret
Can someone give a line-to-line explaination to what exactly this code is doing?
I read the answer on What is the purpose of .bin folder in node_modules?
Still couldn't figure out how this code does all that is stated in the answers.
node version - 12.10.0
npm version - 6.10.3

Why is `npm install` adding all these extra script files to my IntelliJ IDEA project directory?

As of yesterday, whenever I npm install in IDEA, all these extra script files (the red files) get added to my project directory.
Here is what's in uuid:
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/node_modules/uuid/bin/uuid" "$#"
ret=$?
else
node "$basedir/node_modules/uuid/bin/uuid" "$#"
ret=$?
fi
exit $ret
and in uuid.cmd:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\node_modules\uuid\bin\uuid" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\node_modules\uuid\bin\uuid" %*
)
It's annoying and it really clutters up my project. This just started happening and I do not remember changing any settings in IDEA. Why is this happening and/or how can I make it stop?
Update: I think this may have something to do with node/npm global settings but I'm not sure.

Check if package.json has script with a certain name in shell script without using extra NPM packages

I am testing a larger library of NPM packages, that consists of private packages, altered forks of public packages or downstreams of public packages.
lib
|-package_1
|-package_2
|-package_N
So I am running a shell script through my package lib, that runs in each directory the npm test command.
for D in *; do
if [ -d "${D}" ]; then
echo "================================="
echo "${D}" # PRINT DIRECTORY NAME
echo "================================="
cd $D
npm run tests
cd ../ # LEAVE PACKAGE DIR
fi
done
Unfortunately there is not a unique pattern for naming the tests-script in the package's JSON files. Some package are running under test a script with watch-mode and have a different name for their cli script (mostly named testcli).
What I would like to do is something like the following pseudocode:
if has-testcli-script then
npm run testcli
else
npm run test
I assume for now, that only those two options exist. I am rather interested in the way of knowing if the script exists, without installing an additional global NPM package.
Since npm version 2.11.4 at least, calling npm run with no arguments will list all runable scripts. Using that you can check to see if your script is present. So something like:
has_testcli_script () {
[[ $(npm run | grep "^ testcli" | wc -l) > 0 ]]
}
if has_testcli_script; then
npm run testcli
else
npm test
fi
Or alternatively, just check to see if your script is in the package.json file directly:
has_testcli_script () {
[[ $(cat package.json | grep "^ \"testcli\":" | wc -l) > 0 ]]
}
In my case that approach didn't work and I had to implement something using jq instead.
has_script (script) {
[[ ! -z "$(jq -rc --arg key $script '.scripts | to_entries | .[] | select(.key == \$key)' package.json)" ]]
}
then use it as:
if has_script('testcli'); then
// Do something
else
// Do nothing
fi

how to run .sh file on windows using cygwin?

I am beginner of cygwin terminal. I am trying to run *.sh file on windows 8 using command ./file_name.sh, but it gives error given below....
Using prebuilt externals
ERROR: Cannot find 'make' program. Please install Cygwin make package
or define the GNUMAKE variable to point to it.
I have installed cygwin in F drive, I google this error and set the variable path in computer properties > advance system properties > variable environment > path >edit and variable path is ;F:\cygwin\bin
But does not work. How can i solve this problem??
Here is my script
`# set params`
NDK_ROOT=/cygdrive/f/Android/android-ndk-r9b
COCOS2DX_ROOT=/cygdrive/f/Android/cocos2d-2.0-rc2-x-2.0.1
GAME_ROOT=$COCOS2DX_ROOT/molatx
GAME_ANDROID_ROOT=$GAME_ROOT/proj.android
RESOURCE_ROOT=$GAME_ROOT/Resources
buildexternalsfromsource=
usage(){
cat << EOF
usage: $0 [options]
Build C/C++ native code using Android NDK
OPTIONS:
-s Build externals from source
-h this help
EOF
}
while getopts "s" OPTION; do
case "$OPTION" in
s)
buildexternalsfromsource=1
;;
h)
usage
exit 0
;;
esac
done
`# make sure assets is exist`
if [ -d $GAME_ANDROID_ROOT/assets ]; then
rm -rf $GAME_ANDROID_ROOT/assets
fi
mkdir $GAME_ANDROID_ROOT/assets
`# copy resources`
for file in $RESOURCE_ROOT/*
do
if [ -d "$file" ]; then
cp -rf "$file" $GAME_ANDROID_ROOT/assets
fi
if [ -f "$file" ]; then
cp "$file" $GAME_ANDROID_ROOT/assets
fi
done
`# copy icons (if they exist)`
file=$GAME_ANDROID_ROOT/assets/Icon-72.png
if [ -f "$file" ]; then
cp $file $GAME_ANDROID_ROOT/res/drawable-hdpi/icon.png
fi
file=$GAME_ANDROID_ROOT/assets/Icon-48.png
if [ -f "$file" ]; then
cp $file $GAME_ANDROID_ROOT/res/drawable-mdpi/icon.png
fi
file=$GAME_ANDROID_ROOT/assets/Icon-32.png
if [ -f "$file" ]; then
cp $file $GAME_ANDROID_ROOT/res/drawable-ldpi/icon.png
fi
if [[ $buildexternalsfromsource ]]; then
echo "Building external dependencies from source"
$NDK_ROOT/ndk-build -C $GAME_ANDROID_ROOT \
NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/source
else
echo "Using prebuilt externals"
$NDK_ROOT/ndk-build -C $GAME_ANDROID_ROOT \
NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/prebuilt
fi
You have to install make package using cygwin "setup.exe" wizard.
Check what programming language is your script compiling because the compiler of that language will be also dependencies of your script.
Probably you will need to install some libraries too.
Your Windows system path is different to your Cygwin path. If you install the required packages using cygwin setup they will already be available in your $PATH.
Run cygwin setup an install: make , automake , gcc , gcc-c++

Resources