NPM preinstall script

痞子三分冷 提交于 2019-11-29 06:43:36

You are right the preinstall script runs only when we do npm install and there is currently no way to run a script before installing a module but you can use shell scripting and npm view https://docs.npmjs.com/cli/view to do so .

First , create a moduleinstall.sh file which has the same scope as your package.json and the below shell script to it

echo 'Enter the name of the module'
read module_name
npm view $module_name version 
echo "Do you want to install this version(y/N) " 
read option 
if [ "$option" = "N" ] || [ "$option" = "n" ]
then 
echo "exiting...."
exit 1
else
npm install $module_name
fi

make sure you make it executable using chmod +x moduleinstall.sh and then write this in your package.json

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"info": "./moduleinstall.sh"
}

Now you just have to run the command npm run info and then follow the instruction to install a module by checking the version . You can advance it using different options of npm view and shell scripting. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!