Postinstall of module is executed before postinstall of sub-module

Viewed 615

Edit:

I'm asking how to install it from GitHub, not npm, that the question.

In case you want to know why: I'm working on some private projects and don't what t publish the code. This leads to the code being in a private repository, not published on npm.

Original post:

Trying to install a module from GitHub (it is in typescript, thus I need a compilation script [postinstall]). It seems to fail because one of the module dependencies is also on GitHub, and that postinstall script is not waited on by the current postinstall script.

Old GitHub issue that sounds related: https://github.com/npm/npm/issues/5001

Yes, like the issue, I also use the same dependency in the main project as the module does.

Program Version
node v16.1.0
npm 7.11.2
npm info run newton@1.0.1 postinstall node_modules/newton tsc -d -p .
npm info run nodemon@2.0.7 postinstall node_modules/nodemon node bin/postinstall || exit 0
npm info run rendery@2.0.0 postinstall node_modules/rendery tsc -d -p .
npm info run nodemon@2.0.7 postinstall { code: 0, signal: null }
npm timing build:run:postinstall:node_modules/nodemon Completed in 157ms
npm info run newton@1.0.1 postinstall { code: 0, signal: null }
npm timing build:run:postinstall:node_modules/newton Completed in 2409ms
npm info run rendery@2.0.0 postinstall { code: 2, signal: null }
npm timing reify:rollback:createSparse Completed in 2864ms
npm timing reify:rollback:retireShallow Completed in 0ms
npm timing command:install Completed in 47807ms
npm verb stack Error: command failed
npm verb stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\promise-spawn\index.js:64:27)      
npm verb stack     at ChildProcess.emit (node:events:365:28)
npm verb stack     at maybeClose (node:internal/child_process:1067:16)
npm verb stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
npm verb pkgid rendery@2.0.0
npm verb cwd C:\Users\Elias\git\natrias
npm verb Windows_NT 10.0.19043
npm verb argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "i" "--verbose" "--force"    
npm verb node v16.1.0
npm verb npm  v7.11.2
npm ERR! code 2
npm ERR! path C:\Users\Elias\git\natrias\node_modules\rendery
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c tsc -d -p .
npm ERR! src/2d/CanvasRenderyContext2D.ts(1,29): error TS2307: Cannot find module 'newton/2d/ReadonlyVector2' or its corresponding type declarations.
npm ERR! src/2d/CanvasRenderyContext2D.ts(2,21): error TS2307: Cannot find module 'newton/2d/Vector2' or its corresponding type declarations.    
npm ERR! src/2d/ReadonlyRenderyContext2D.ts(1,29): error TS2307: Cannot find module 'newton/2d/ReadonlyVector2' or its corresponding type declarations.
npm ERR! src/2d/RenderyContext2D.ts(1,29): error TS2307: Cannot find module 'newton/2d/ReadonlyVector2' or its corresponding type declarations.  
npm verb exit 2
npm timing npm Completed in 48318ms
npm verb code 2

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Elias\AppData\Local\npm-cache\_logs\2021-05-12T16_19_34_975Z-debug.log

Note that rendery postinstall starts before newton postinstall completes.


Newton package.json

{
  "name": "newton",
  "version": "1.0.1",
  "description": "",
  "scripts": {
    "lint": "eslint '*/**/*.ts'",
    "lint:fix": "eslint '*/**/*.ts' --fix",
    "postinstall": "tsc -d -p .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.22.0",
    "@typescript-eslint/parser": "^4.22.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0"
  },
  "dependencies": {
    "typescript": "^4.2.4"
  }
}


Rendery package.json

{
  "name": "rendery",
  "version": "2.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "eslint '*/**/*.ts'",
    "lint:fix": "eslint '*/**/*.ts' --fix",
    "postinstall": "tsc -d -p ."
  },
  "keywords": [],
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.22.0",
    "@typescript-eslint/parser": "^4.22.0",
    "eslint": "^7.25.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-typescript-sort-keys": "^1.6.0"
  },
  "dependencies": {
    "newton": "github:Elias-Graf/newton#main",
    "typescript": "^4.2.4"
  }
}

1 Answers

It's not a good practice to publish unbuilt packages. I recommend changing the postinstall script into a prepare script so it builds the package before publishing it.

{
  "name": "newton",
  "version": "1.0.1",
  "description": "",
  "scripts": {
    "lint": "eslint '*/**/*.ts'",
    "lint:fix": "eslint '*/**/*.ts' --fix",
    "prepare": "tsc -d -p .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.22.0",
    "@typescript-eslint/parser": "^4.22.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0"
  },
  "dependencies": {
    "typescript": "^4.2.4"
  }
}
{
  "name": "rendery",
  "version": "2.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "eslint '*/**/*.ts'",
    "lint:fix": "eslint '*/**/*.ts' --fix",
    "prepare": "tsc -d -p ."
  },
  "keywords": [],
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.22.0",
    "@typescript-eslint/parser": "^4.22.0",
    "eslint": "^7.25.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-typescript-sort-keys": "^1.6.0"
  },
  "dependencies": {
    "newton": "github:Elias-Graf/newton#main",
    "typescript": "^4.2.4"
  }
}

This would fix your issue.

Related