How to run a script before installing any npm module? - javascript

I am trying to make a test for npm packages for my project such that every time I try to install a module i.e run npm install <module> a script must be run before that module is installed. The preinstall script only works with npm install and not with npm install <module>.
eg :- If run npm install request . It should run a script which shows me all the dependencies of the request module before installing the module. Thanks in advance.

Add "install": "[Your Command]" in the script part of your package.json
Example:
{
"name": "test",
"version": "1.0.0",
"description": "A sample test",
"main": "index.js",
"scripts": {
"install": "echo Test"
}
}
You can also use a pre hook with "preinstall": "[Your Command]"

Related

I am getting 404 error while running "npm publish"

I am trying to publish a package to the npm registry. But when I hit the command npm publish I get this error.
npm WARN prepublish-on-install As of npm#5, `prepublish` scripts are deprecated.
npm WARN prepublish-on-install Use `prepare` for build steps and `prepublishOnly` for upload-only.
npm WARN prepublish-on-install See the deprecation note in `npm help scripts` for more information.
> imojha#1.0.0 prepublish .
> npm run build
> imojha#1.0.0 build /home/suraj/Projects/bitandbang
> node build.js
npm notice
npm notice 📦 imojha#1.0.0
npm notice === Tarball Contents ===
npm notice 1.1kB LICENSE
npm notice 3.6kB bin/output
npm notice 233B bin/card.js
npm notice 996B package.json
npm notice 293B README.md
npm notice === Tarball Details ===
npm notice name: imojha
npm notice version: 1.0.0
npm notice package size: 2.3 kB
npm notice unpacked size: 6.2 kB
npm notice shasum: bb283ae5fe8aed311771f369866c13e24f1eb937
npm notice integrity: sha512-Nzc+Ysmf4RgSi[...]XoT+OGYHNHoSQ==
npm notice total files: 5
npm notice
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://npm.registry.github.com/imojha
npm ERR! 404
npm ERR! 404 'imojha#1.0.0' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/suraj/.npm/_logs/2020-10-24T14_54_01_996Z-debug.log
I have successfully run npm login. Then I am executing npm publish.
I have cloned this repo from GitHub and publishing to the npm. It's actually about creating npx card of your own name, like npx username. I am doing the same.
I have tried giving it a different package-name but couldn't make it. So, Could someone please tell me what I am doing wrong? Here is the package.json file.
{
"name": "suraj-ojha",
"version": "1.0.0",
"description": "A personal card for Suraj Ojha (#suraj)",
"main": "/bin/card.js",
"bin": {
"bitandbang": "./bin/card.js"
},
"repository": {
"type": "git",
"url": "git#github.com:Suraez/npxcard.git"
},
"homepage": "https://bnb.im",
"scripts": {
"prepublish": "npm run build",
"build": "node build.js",
"dev": "npm run build && node ./bin/card.js",
"lint": "standard",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"card",
"npm",
"npm card",
"npx",
"npx card",
"business card"
],
"author": "suraj Ojha",
"license": "MIT",
"files": [
"bin/card.js",
"bin/output"
],
"devDependencies": {
"boxen": "^2.1.0",
"chalk": "^2.4.1",
"standard": "^12.0.1"
},
"bugs": {
"url": "https://github.com/bnb/bitandbang/issues"
},
"dependencies": {},
"publishConfig": {
"registry": "https://npm.registry.github.com/"
}
}
Update: I believe its caused from issues from the .npmrc in your home folder. Messing with the $HOME variable also seems to make it fail. Removing that file, and logging back into npm seems to fix the issue.
Beyond the answer though, Suraj Oberai your question is very well worded with all the necessary information, I don't think you deserve your downvotes.
I've published many npm packages, but this was the first time I had come across this issue myself. It was very strange as there was almost no difference between this one and previous packages I've published. I had to track down the issue by taking one of my working packages and (many useless publishes later) slowly convert it into my attempt that was causing the error, only to get to the point where literally nothing different between the projects. Nothing except the HOME Env variable of the shell.
This is definitely a bug with npm. It seems like the error above is the default error, so its possible my change might not solve this issue for you.
The publishConfig URL might be wrong.
Can you try adding the publishConfig option in package.json:
"publishConfig": {
"registry":"https://npm.pkg.github.com"
},
It worked for with this other registry (maybe they updated it?):
"publishConfig": {
"registry": "https://registry.npmjs.org/"
}
GitHub NPM registry is used for private packages. The package name has to be scoped. "name": "suraj-ojha" won't work for GitHub. It needs to be something like #suraj/ojha or #bnb/bitandbang (since that's your GitHub username and repo name).
If you don't intend to publish a private package and want to publish it to the public, remove the publishConfig section in package.json so npm publish uses the default public registry.

npm not running scripts in package.json

The problem is that when I do npm start OR npm run customScriptCommand npm just not doing anything with the project and quickly just return new line in the terminal.
I've tried removing node and npm from my machine and then do brew installation for node and npm, but it does not fix the problem.
Then I tried removing node and npm from the brew installation and installing it again from nvm, but it also does not fix the problem.
NPM Details
npm -v -> 6.11.3
which npm -> /usr/local/bin/npm
NodeJS Details
node -v -> v12.12.0
which node ->/usr/local/bin/node
Scripts in package.json
"scripts": {
"start": "node ./bin/www",
"devstart": "DEBUG=app-name:* nodemon ./bin/www", //I've changed my actual app name to "app-name"
},
If I do node ./bin/www OR DEBUG=app-name:* nodemon ./bin/www it will work:
Update
I've tried on other project that does not have problem on my colleague's machine, with git clone do npm install and tried to run the project, but it still failed
Even with fresh project which I just did npm init -y it fail,
{
"name": "test"
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "itsme",
"license": "ISC"
}
npm config set ignore-scripts false would do the trick for you.
The struggle is real my friend, not following random tutorials from now.
https://github.com/npm/cli/issues/541
Installing npm install --save-dev nodemon and running npm start did the magic for me
You can install yarn and it works; just run yarn start. As to what caused the problem I have no idea.

How to make npx work with my custom npm pakage?

I've published a new package which aims to generate a very small boilerplate for a node open source project via cli. For now, it's just a combination of few npx commands and requires other npm packages like gitignore, license to work. I want to execute the build script in package.json with the following command.
npx get-set-node-oss build youremailaddress#xyz.com
Link to the npm package: get-set-node-oss. I know the name is a bit too long.
{
"name": "get-set-node-oss",
"version": "1.0.1",
"description": "One command setup for your Node OSS project",
"scripts": {
"build": "npx license mit > LICENSE && npx gitignore node && npx covgen"
},
"author": "Harshit Juneja",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/harshitjuneja/get-set-node-oss.git"
},
"keywords": [
"node",
"boilerplate","gitignore","MIT","OSS"
],
"bugs": {
"url": "https://github.com/harshitjuneja/get-set-node-oss/issues"
},
"homepage": "https://github.com/harshitjuneja/get-set-node-oss#readme"
}
I expect the user to make a new folder and cd into the folder and do
npx get-set-node-oss build emailstring
and get the resulting boilerplate files.
For npx to work you need to specify what the get-set-node-oss command means. Thankfully, this can be accomplished by using the bin field in your package.json file. More information from NPM documentation for your reference:
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
An example of it below.
"bin": {
"get-set-node-oss": //script you like to run
},
Hope it works out for you. I have understood bin as scripts in package.json. Where we can either do npm run foo or npx foo both yielding the same results. If you want an example, here you go https://github.com/vipulgupta2048/balenaclone

npm ERR Refusing to install package with name <packageName> under a package also called <packageName>

I am trying to use the jsonfile package in my project, but I get the following errors:
Refusing to install package with name <packagename> under a package also called <packagename>. (Note that none of the directory or filename is same as package name)
Cannot find module <packagename>.
The problem is caused when the name of project in package.json is the same as the module you're trying to install.
To solve this problem, please change the project name in package.json to something else. For example, "jsonfile-test":
{
"name": "jsonfile-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
I think it should be:
npm install --save json-file
not
npm install --save jsonfile
ref: https://www.npmjs.com/package/json-file
If that does not work please try enabling permissions install - e.g. (on mac- but will be equivalent on windows, check: https://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/) run:
sudo npm install --save json-file

'var' is not defined as internal or external command in npm

I am trying to install histogramjs package. npm i histogramjs --->works well but when I try to run --> var hist = require('histogramjs')
I got the error on cmd:
'var' is not defined as internal or external command in npm
I have tried already tried
npm install var -- >get installed abd when I try to run ---> var env = require('var');
get the same error..
If run directly from node.js , get error 'cannot find module 'var'
Please help as I am very new to npm and node
Thanks
You should not type in var hist .... in your command line. What you type there, will be executed by shell, not by npm/node.
Instead, try:
Run npm init. Press enter couple of times, until it completes. This will create package.json file which is npm's configuration file. this file will tell npm, among others, which file is an entry point to your application.
Edit package.json, add "start": "node app.js", inside "scripts" element (see example below).
Create app.js in the same directory where package.json is located. Put your script in this file.
Run npm start.
If your script depends on 3rd party modules (in your case - "histogramjs" probably), install those as well:
npm install --save histogramjs
Running it without --save modifier will work as well. But --save will cause npm to put "histogramjs" as a dependency in package.json. Thanks to that, when someone gets your code later (i.e. from repository), he'll be able to simply run npm install, without even having to care which dependencies are required.
package.json
{
"name": "t",
"version": "0.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD"
}
app.js
var hist = require('histogramjs')
// ... rest of your code

Categories

Resources