[npm] Dependencies of optionalDependencies are not installed
Root Cause
- Npm says dependencies of
file:folder references are not installed but just symlinks to the file:folder packages are created AS DESIGNED
- This behavior is different in NPM 6
Workarounds
- OPTION # 1: Use npm version 6 and perform
npm i twice
- OPTION # 2: Use a tgz package file instead of
thin-hook package on the repo as follows:
- Note: This behavior may change in the future npm
cd $PROJECTDIRECTORY
rm -v thin-hook-*.tgz # clean up archives
npm pack thin-hook # create the latest archive such as thin-hook-0.4.0-alpha.57.tgz
npm i thin-hook-*.tgz # install from the tgz archive
npm i # install optionalDependencies and dependencies of the optionalDependencies
rm -v thin-hook-*.tgz # clean up archives
- OPTION # 3: Install as a tgz package via
package.json scripts
- Notes
--save-optional avoids errors at the first installation
- In
postinstall script, npm i is called recursively once
{
"scripts": {
"preinstall": "if [ ! -f thin-hook-0.4.0-alpha.57.tgz ]; then npm pack thin-hook@0.4.0-alpha.57; fi",
"postinstall": "if [ ! -f node_modules/thin-hook/package.json ]; then npm i --save-optional thin-hook-0.4.0-alpha.57.tgz; npm i; else if [ ! -f node_modules/reportage/package.json ]; then npm i; fi; fi",
},
}
- OPTION # 4: Copy nested plugin dependencies explicitly after
npm i thin-hook@^0.4.0-alpha.57 --install-links=true
- Notes:
npm i again since package-lock.json is (strangely) inconsistent after the npm i thin-hook@^0.4.0-alpha.57 --install-links=true installation
- Verified with
npm@9.8.1
- If
npm i --install-links=true is executed without the explicit thin-hook package name, the plugins are unexpectedly installed at node_modules/thin-hook/node_modules/@thin-hook/* instead of the expected node_modules/@thin-hook/*, which is the reason why the explicit package name is required
- There is, of course, another option that the nested dependencies are copied from
node_modules/thin-hook/node_modules/@thin-hook/*/node_modules instead of node_modules/@thin-hook/*/node_modules.
- As of
0.4.0-alpha.57, the plugin with any nested dependencies that have to be explicitly copied is only @thin-hook/injector-helpers
{
"scripts": {
"preinstall": "if [ ! -f node_modules/reportage/package.json ]; then npm i thin-hook@0.4.0-alpha.57 --install-links=true; fi",
"install": "cd node_modules/@thin-hook/; for i in *; do { if [ -d $i/node_modules ] && [ ! -d ../thin-hook/plugins/$i/node_modules ]; then cp -rv $i/node_modules ../thin-hook/plugins/$i/; fi; } done;",
"postinstall": "grep '\"resolved\": \"file:node_modules/thin-hook/plugins/about-blank-redirector\"' package-lock.json; if [ \"$?\" = \"0\" ]; then npm i; fi",
},
"dependencies": {
"thin-hook": "0.4.0-alpha.57"
}
}
[npm] Dependencies of optionalDependencies are not installed
Root Cause
file:folderreferences are not installed but just symlinks to thefile:folderpackages are created AS DESIGNEDWorkarounds
npm itwicethin-hookpackage on the repo as follows:package.jsonscripts--save-optionalavoids errors at the first installationpostinstallscript,npm iis called recursively once{ "scripts": { "preinstall": "if [ ! -f thin-hook-0.4.0-alpha.57.tgz ]; then npm pack thin-hook@0.4.0-alpha.57; fi", "postinstall": "if [ ! -f node_modules/thin-hook/package.json ]; then npm i --save-optional thin-hook-0.4.0-alpha.57.tgz; npm i; else if [ ! -f node_modules/reportage/package.json ]; then npm i; fi; fi", }, }npm i thin-hook@^0.4.0-alpha.57 --install-links=truenpm iagain sincepackage-lock.jsonis (strangely) inconsistent after thenpm i thin-hook@^0.4.0-alpha.57 --install-links=trueinstallationnpm@9.8.1npm i --install-links=trueis executed without the explicit thin-hook package name, the plugins are unexpectedly installed atnode_modules/thin-hook/node_modules/@thin-hook/*instead of the expectednode_modules/@thin-hook/*, which is the reason why the explicit package name is requirednode_modules/thin-hook/node_modules/@thin-hook/*/node_modulesinstead ofnode_modules/@thin-hook/*/node_modules.0.4.0-alpha.57, the plugin with any nested dependencies that have to be explicitly copied is only@thin-hook/injector-helpers{ "scripts": { "preinstall": "if [ ! -f node_modules/reportage/package.json ]; then npm i thin-hook@0.4.0-alpha.57 --install-links=true; fi", "install": "cd node_modules/@thin-hook/; for i in *; do { if [ -d $i/node_modules ] && [ ! -d ../thin-hook/plugins/$i/node_modules ]; then cp -rv $i/node_modules ../thin-hook/plugins/$i/; fi; } done;", "postinstall": "grep '\"resolved\": \"file:node_modules/thin-hook/plugins/about-blank-redirector\"' package-lock.json; if [ \"$?\" = \"0\" ]; then npm i; fi", }, "dependencies": { "thin-hook": "0.4.0-alpha.57" } }