Home > Back-end >  How to properly run npm install scripts?
How to properly run npm install scripts?

Time:05-03

I'm trying to execute a Node.js script after my package is installed. But even testing with a simple echo command fails already. Because I'm testing with local installation first, I've also tried the prepare script instead of install, but get the same results: no execution.

Suppose I have Package A (PA) that I want to install into Package B (PB).

PA's package.json script part looks like the following (tried install and prepare): "install": "echo INSTALLED"

Installing it in PB like so: npm install --save-dev ../package-a

But the echo, that should show something in console, seems to do nothing.

What do I oversee?

Update 1

Here's the verbose output of npm-install:

$ npm install --save-dev --loglevel verbose ../package-a/
npm verb cli [
npm verb cli   'C:\\Program Files\\nodejs\\node.exe',
npm verb cli   'C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
npm verb cli   'install',
npm verb cli   '--save-dev',
npm verb cli   '--loglevel',
npm verb cli   'verbose',
npm verb cli   '../package-a/'
npm verb cli ]
npm info using [email protected]
npm info using [email protected]
npm timing npm:load:whichnode Completed in 0ms
npm timing config:load:defaults Completed in 5ms
npm timing config:load:file:C:\Users\User\AppData\Roaming\npm\node_modules\npm\npmrc Completed in 2ms
npm timing config:load:builtin Completed in 2ms
npm timing config:load:cli Completed in 2ms
npm timing config:load:env Completed in 1ms
npm timing config:load:file:F:\public\npm\package-b\.npmrc Completed in 0ms
npm timing config:load:project Completed in 2ms
npm timing config:load:file:C:\Users\User\.npmrc Completed in 1ms
npm timing config:load:user Completed in 1ms
npm timing config:load:file:C:\Users\User\AppData\Roaming\npm\etc\npmrc Completed in 1ms
npm timing config:load:global Completed in 1ms
npm timing config:load:validate Completed in 0ms
npm timing config:load:credentials Completed in 1ms
npm timing config:load:setEnvs Completed in 0ms
npm timing config:load Completed in 16ms
npm timing npm:load:configload Completed in 16ms
npm timing npm:load:setTitle Completed in 0ms
npm timing config:load:flatten Completed in 3ms
npm timing npm:load:display Completed in 7ms
npm verb logfile C:\Users\User\AppData\Local\npm-cache\_logs\2022-05-02T13_10_48_164Z-debug-0.log
npm timing npm:load:logFile Completed in 17ms
npm timing npm:load:timers Completed in 1ms
npm timing npm:load:configScope Completed in 0ms
npm timing npm:load Completed in 42ms
npm timing arborist:ctor Completed in 1ms
npm timing idealTree:init Completed in 237ms
npm timing idealTree:userRequests Completed in 26ms
npm verb shrinkwrap failed to load node_modules/.package-lock.json missing from node_modules: ../package-c
npm timing idealTree:#root Completed in 16ms
npm timing idealTree:node_modules/package-a Completed in 0ms
npm timing idealTree:buildDeps Completed in 18ms
npm timing idealTree:fixDepFlags Completed in 3ms
npm timing idealTree Completed in 292ms
npm timing reify:loadTrees Completed in 560ms
npm timing reify:diffTrees Completed in 13ms
npm timing reify:retireShallow Completed in 0ms
npm timing reify:createSparse Completed in 3ms
npm timing reify:loadBundles Completed in 0ms
npm verb reify failed optional dependency F:\public\npm\package-b\node_modules\fsevents
npm timing reifyNode:node_modules/fsevents Completed in 21ms
npm timing reifyNode:node_modules/package-a Completed in 24ms
npm timing reify:unpack Completed in 24ms
npm timing reify:unretire Completed in 0ms
npm timing build:queue Completed in 0ms
npm timing build:deps Completed in 2ms
npm timing build:queue Completed in 1ms
npm timing build:links Completed in 1ms
npm timing build Completed in 4ms
npm timing reify:build Completed in 5ms
npm timing reify:trash Completed in 1ms
npm timing reify:save Completed in 64ms
npm http fetch POST 200 https://registry.npmjs.org/-/npm/v1/security/advisories/bulk 422ms
npm timing auditReport:getReport Completed in 429ms
npm timing auditReport:init Completed in 0ms
npm timing reify:audit Completed in 430ms
npm timing reify Completed in 1015ms

added 1 package, and audited 480 packages in 1s

58 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm timing command:install Completed in 1022ms
npm verb exit 0
npm timing npm Completed in 1383ms
npm info ok

Update 2

I've noticed I can run npm install . inside PA and my NPM script is executed. So the question here is: How do I get scripts executed if package is installed by another one?

CodePudding user response:

Try naming your script postinstall, so the script runs automatically after issuing npm install:

"scripts": {
  ...
  "postinstall": "echo INSTALLED"
}

CodePudding user response:

Ok, I still don't understand why, but my solution to get it up and running is:

  • If using a local path:
    1. Use prepare instead of install or postinstall
    2. prepare seems to only execute if there is also an install script available in package.json.
    3. install may not be empty. So an echo or such is at least required.
  • If using local *.tgz package use install instead (maybe just run prepare)

As said, I still don't know why it is working like that, because I always understood that behaviour a bit more different than it seems to be.

So further explanation of someone who knows why it only works like that is highly appreciated.

  • Related