Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a post-install script after individual execution of "npm install <package>"

I am maintaining the following directory structure:

/home/user/Desktop/
                 |-- app/
                 |      |-- package.json
                 |      `-- server.js
                 |-- node/
                 |      |-- bin/
                 |      |      |-- node
                 |      |      `-- npm
                 |      |-- include/
                 |      |-- lib/
                 |      `-- share/
                 |
                 `-- npm.sh

I want all my locally installed node modules reside in the directory node. That is, if I run npm install inside the directory app, initially it'll install the modules inside the current directory (app) and then move the node_modules folder to the external directory called node. For this purpose I've written a script npm.sh and placed the mv (move) command inside the postinstall script of package.json.

These are the files npm.sh and package.json.

content of npm.sh:

#/bin/bash

export PATH=/home/user/Desktop/node/bin:$PATH
export NODE_PATH=/home/user/Desktop/node/node_modules
export NODE_MODULE_ROOT=/home/user/Desktop/node
/bin/bash

content of app/package.json:

{
  "name": "app",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "mv node_modules $NODE_MODULE_ROOT",
    "start": "node server.js"
  },
  "dependencies": {
    "jwt-simple": "^0.5.1"
  }
}

But the problem is: when I do ./npm.sh && cd app && npm install, everything works as intended. But when I do npm install jwt-simple, the postinstall script is not getting executed.

Is there a way to make it work for individual npm install <package> ? Or is there any better way to accomplish this ?

like image 795
dibyendu Avatar asked Jun 05 '17 05:06

dibyendu


People also ask

How do I run a script after npm install?

You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.

What is the first lifecycle script that will be executed when you use the npm install command?

Life Cycle Operation Order js file in the root of your package, then npm will default the start command to node server. js . prestart and poststart will still run in this case.

What happens after npm install?

The npm install installs all modules that are listed on package. json file and their dependencies. npm update updates all packages in the node_modules directory and their dependencies.


2 Answers

You can use npm hook scripts to do something after package is installed.

Create node_modules/.hooks/postinstall executable and it will be run also after npm install <package>.

NOTE: I have noticed problems with npm hook scripts between npm version 5.1.0 until 6.0.1. So if you have problems with hooks, check your npm version and upgrade if necessary.

like image 180
Niko Ruotsalainen Avatar answered Oct 14 '22 12:10

Niko Ruotsalainen


For anyone else stumbling here npm doesn't run pre/postinstall in the package.json when installing a specific package. You can check here for reference, https://npm.community/t/preinstall-npm-hook-doesnt-execute-when-installing-a-specific-package/2505. Not sure if there is a way around it but I've been looking too.

like image 28
Charles Campbell Avatar answered Oct 14 '22 13:10

Charles Campbell