NPM preinstall script - node.js

I am trying to run some policing script before any packages are installed.
For Example:
{
"name": "pre-hook-check",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"preinstall": "echo preinstall",
"postinstall": "echo postinstall"
},
"author": "",
"license": "ISC",
"dependencies": {
"abc": "^0.6.1",
"preact": "^8.2.5"
}
}
It seems the pre and post install script on above example only works when I do npm install, but I want that to run every time I try to install anything.
For example: Let's say I want to write a script to check for the version of the package any time my team runs npm install <some package>. I want to check for the version of installing package and verify that it's version is above "1.0.0" else don't let them install.
I was planning to write a preinstall script that goes
npm info lodash version
and checks for the version of any package I am trying to install. If the version is not available, I plan to make it interactive and ask user's acknowledgement before install.

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.

Related

Npm ERR! Line breaks can’t be quoted on Windows

i have the following package.json code:
{
"name": "pre-post",
"version": "1.0.0",
"description": "",
"main": "basic-server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"ignore": "echo \"\" >> .gitignore",
"preignore": "touch .gitignore",
"postignore": "echo \".gitignore\n.vscode/\nnode_modules/\" >> .gitignore"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"linebreak": "^1.0.2",
"open": "^7.4.0"
}
}
after i run in node js command, i have the error message of npm ERR! Line breaks can't be quoted on Windows, any fix?
Check whether your package.json have swiper
There are 4 steps to solve this problem
Remove "swiper" : "^5.4.5" from package.json file.(Check there should be no comma in last line of your json file)
Run npm install command in your terminal.
Run npm install swiper command.
Now you will see everything has been installed successfully.
Just don't install it from Git Bash on Windows. Use cmd.exe and it works as a charm !
Uninstall node.js from Control Panel/Programs
Download fresh node.js installer and install them.
(important) reboot system.
I face the same problem and this thing is work for me
Uninstall node completely use this answer
reboot your device
download again from official website
Use yarn to install the dependencies. Solved for me.

can not setup ReactJS on mac

I need to setup ReactJS on my Mac. I have npm version of 4.1.2 and node version of v7.7.4. I clone the project from Git and in the project folder trying to execute npm install and following npm start. But I get error.
I removed&uninstalled node and npm and then re-installed with homebrew, but it did not help. Here is the screen of error I get in terminal? Do you have any idea what is the problem?
Here is my package.json file
{
"name": "viaopt",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"webpack": "^2.3.0"
},
"description": ""
}
From the error you are showing I can tell that there is no start script in your package.json file. For npm start to work there should be a command under the start key under the scripts attribute in your package.json file.
When you run npm start npm looks in package.json and runs whatever is listed there under the start value.
I'm guessing you have gulp or webpack configured with your project?
Try running webpack in your terminal and see if your project fires up or smth.
Hopes this helps.
Cheers.
If your trying to setup a new React App you can use
Create React App. It will setup React / Webpack for you.

NPM install doesn't trigger babel build if dependencies not coming from NPM

For example, if in my package.json, i have this:
"dependencies": {
"cacheman": "2.1.0" }
it works and it will trigger the building script inside cacheman when I do npm install.
however, if i do this:
"dependencies": {
"cacheman": "https://github.com/cayasso/cacheman.git" }
it won't work. npm install will not trigger the build process for cacheman.
why is that?
The script you are referring is pre-publish script which runs before publishing the npm module to npm registry. Check here package.json#L9
Extract shown here
"scripts": {
"test": "make test",
"prepublish": "make"
}
When you install it from github there is no publish step so the script is not run.
If you want to install from github only and have the script run, you can add it as postinstall script of cacheman (you will have to fork the repo to make changes if you are not owner of cacheman).
"scripts": {
"test": "make test",
"prepublish": "make",
"postinstall": "make"//Added postinstall
}
Check examples in npm scripts documentation for more details.

how to automate dependency resolution (local and remote) in node

I have a very small data-cap so I want to install dependencies for node projects by first linking to whatever it finds installed on local machine and fetching remotely if not found locally. I don't want to manually go through all the require statements in the apps because theres a lot of them - is there a node way to do this?
eg. this except not manually determining what is or isn't installed
Project A
npm install -g connect
npm install -g serve-static
then later
Project B
npm link connect
npm link serve-static
The preferred way to go about dependency management is with a package.json.
To get started with one of these in a project, run npm init, and answer all the questions. This will leave you with a file like this:
{
"name": "test-project",
"version": "1.0.0",
"description": "A test project",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josh",
"license": "ISC"
}
Once this is in place, add the --save flag to each of your npm install commands. For example, if you run npm install --save connect, your package.json changes to this
{
"name": "test-project",
"version": "1.0.0",
"description": "A test project",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Josh",
"license": "ISC",
"dependencies": {
"connect": "^3.3.5"
}
}
Now whenever you need to install the project's dependencies, run npm install, and all the dependencies listed in the package.json will be automatically installed.

npm WARN install Refusing to install hapi as a dependency of itself

I tried to do the following (per instructions from official site):
mkdir hapi && cd hapi
npm init
npm install hapi --save
But this gives me an error like this:
npm WARN install Refusing to install hapi as a dependency of itself
Now, I made a new test folder called hapiTest and repeated the commands and then everything worked fine.
I tried the same process with a folder gulp and npm install gulp --save, and got the same error, so my conclusion is that I can't have the name of the folder be the same as the package that I want to install, but can someone back this statement up with some official documentation?
When you did the command npm init, there were probably some relevant questions you needed to answer. Specifically, the name of your module. When you use npm init, it assumes you want the name of the module you're creating to be called the name of the folder it is in.
So it's not the name of the folder that is stopping you from installing the dependency, it is the name of the npm module that you are creating.
Open the resulting package.json within your hapi directory, and rename the module to something other than hapi. Here's an example 'package.json' that works, even when residing in a folder called hapi:
{
"name": "hapi-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"hapi": "^7.5.2"
}
}
Added Note
I've not been able to find any documentation thus-far explaining this phenomena in the context of npm; though it is a bit of a no-brainer. Requiring modules with the same name within the same application would conflict with the CommonJS philosophy.
The name of your module is same as the module you are trying to install. NPM thinks that you are installing the module to itself. Change the name of your module and it will install perfectly.
Reason
Module name is same with library name
Solution
Change the module name to something else
Change 'name' in package.json
The issue can be simply explained as follows
the name of your package or module in package.json cannot be same as that of the package or module you are trying to install.
Here hapi is the name of your module and you are trying to install a module with name hapi with npm install hapi --save
This was my initial code
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
which threw error
npm WARN package.json react#1.0.0 No description
npm WARN package.json react#1.0.0 No repository field.
npm WARN package.json react#1.0.0 No README data
npm WARN install Refusing to install react as a dependency of itself
then i renamed the name from react to react_app and my code looks like
{
"name": "react_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
then it worked

Resources