rdkcentral / Lightning-SDK

SDK for Lightning framework
Apache License 2.0
130 stars 69 forks source link

postinstall script fails #140

Closed jhderojasUVa closed 4 years ago

jhderojasUVa commented 4 years ago

Hi,

The postinstall script at installation is failing on the file copy time when upgraded from SDK 2 to SDK 3.

For a cleaner installation I remove the node_modules directory and remove the package-lock.json file. In this case I have this error:

The directory where I have the project is /projects/develop

> @lightningjs/sdk@3.0.0 postinstall /projects/develop/XXXXXXXXXr/node_modules/wpe-lightning-sdk
> node ./scripts/postinstall.js

mkdir: path already exists: /projects/develop/XXXXXXXXX/node_modules/wpe-lightning-sdk/support
cp: no such file or directory: /projects/develop/XXXXXXXXX/@lightningjs/core/devtools/*
cp: no such file or directory: /projects/develop/XXXXXXXXX/@lightningjs/core/dist/*
cp: no such file or directory: /projects/develop/XXXXXXXXX/url-polyfill/url-polyfill.js
cp: no such file or directory: /projects/develop/XXXXXXXXX/@babel/polyfill/dist/polyfill.js
cp: no such file or directory: /projects/develop/XXXXXXXXX/whatwg-fetch/dist/fetch.umd.js

So I guess that on the postinstall.js the lines that copy the files are incorrect because the line 26 is the responsible of put the node_modules directory of the project and not the main directory.

So, (I have no tested), must be:

const nodeModulesFolder = path.join( process.cwd(), process.cwd().indexOf('node_modules') > -1 ? '../../node_modules' : 'node_modules' )

With addiding the node_modules at the two step directory back.

Other problem is that if you don't remove the package-lock and the node_modules directory you will have this error:

> wpe-lightning-sdk@github:rdkcentral/Lightning-SDK#c8b0bfbd623e84063e73246ecc32406acddb08bc postinstall /projects/develop/XXXXXXXXX/node_modules/wpe-lightning-sdk
> node ./scripts/postinstall.js

internal/modules/cjs/loader.js:1023
  throw err;
  ^

Error: Cannot find module 'replace-in-file'
Require stack:
- /projects/develop/XXXXXXXXX/node_modules/wpe-lightning-sdk/scripts/postinstall.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1020:15)
    at Function.Module._load (internal/modules/cjs/loader.js:890:27)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/projects/develop/XXXXXXXXX/node_modules/wpe-lightning-sdk/scripts/postinstall.js:23:23)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/projects/develop/XXXXXXXXX/node_modules/wpe-lightning-sdk/scripts/postinstall.js'
  ]
}

That seems that the required replace-in-file is not there and it's needed on the same script (line 23(. The solution is to do an npm i --save-dev replace-in-file.

But, if you do that you have a new error:

UKC02Z7491LVCF:XXXXXXX jhz02$ npm i --save-dev replace-in-file

> wpe-lightning-sdk@github:rdkcentral/Lightning-SDK#c8b0bfbd623e84063e73246ecc32406acddb08bc postinstall /projects/develop/XXXXXXX/node_modules/wpe-lightning-sdk
> node ./scripts/postinstall.js

internal/modules/cjs/loader.js:1023
  throw err;
  ^

Error: Cannot find module 'yesno'
Require stack:
- /projects/develop/XXXXXXX/node_modules/wpe-lightning-sdk/scripts/postinstall.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1020:15)
    at Function.Module._load (internal/modules/cjs/loader.js:890:27)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/projects/develop/XXXXXXX/node_modules/wpe-lightning-sdk/scripts/postinstall.js:24:15)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/projects/develop/XXXXXXX/node_modules/wpe-lightning-sdk/scripts/postinstall.js'
  ]
}

Because when you install the replace-in-file it triggers again the postinstall.js script and fails because the yesno is not there... having only one solution to do: npm --save-dev yesno replace-in-file (install both)

michielvandergeest commented 4 years ago

hey @jhderojasUVa thanks for your report!

We did run quite some tests to make sure the upgrade from sdk2 to sdk3 would work, but apparently we missed a case 👍 Would you be able to walk me through your steps in upgrading from v2 to v3, so I can try to reproduce here?

Thanks

jhderojasUVa commented 4 years ago

Hi,

It happens to all of my projects and it's easy to reproduce. In fact you can get one of your projects :)

For example: https://github.com/jhderojasUVa/tmdb-challenge4 (you will recognise it)

Steps:

  1. Clone it
  2. npm i You will get the error
    
    > wpe-lightning-sdk@github:rdkcentral/Lightning-SDK#12b9bf32040d398917759626b4f627bac1050fc2 postinstall /projects/test/tmdb-challenge4/node_modules/wpe-lightning-sdk
    > node ./scripts/postinstall.js

internal/modules/cjs/loader.js:1023 throw err; ^

Error: Cannot find module 'replace-in-file' Require stack:

You can "skip" this first problem by doing an npm ci(that removes the node_modules directory, so the postinstall script will not fail) but it will not upgrade your project to SDK 3.

You can say: Oh! in that project you are using an old SDK (1.7.0) with the router feature that was implemented years ago. So you can change the package.json file and change to the 1.9.0 wpe-lighting, and you will get:

UKC02Z7491LVCF:tmdb-challenge4 jhz02$ npm i
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.

> husky@3.1.0 preuninstall /projects/test/tmdb-challenge4/node_modules/husky
> node husky uninstall

husky > Uninstalling git hooks
husky > Done

> core-js@2.6.11 postinstall /projects/test/tmdb-challenge4/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

> @lightningjs/sdk@3.0.0 postinstall /projects/test/tmdb-challenge4/node_modules/wpe-lightning-sdk
> node ./scripts/postinstall.js

mkdir: path already exists: /projects/test/tmdb-challenge4/node_modules/wpe-lightning-sdk/support
cp: no such file or directory: /projects/test/tmdb-challenge4/@lightningjs/core/devtools/*
cp: no such file or directory: /projects/test/tmdb-challenge4/@lightningjs/core/dist/*
cp: no such file or directory: /projects/test/tmdb-challenge4/url-polyfill/url-polyfill.js
cp: no such file or directory: /projects/test/tmdb-challenge4/@babel/polyfill/dist/polyfill.js
cp: no such file or directory: /projects/test/tmdb-challenge4/whatwg-fetch/dist/fetch.umd.js

(and the rest is the common for upgrading asking you to replace the code on your project). You can choose yes (or not, at this point doesn't matter) because it fninsh with ok, also you can do an npm i again with no problems (because @lightningjs is there at node_modules) but when you run the app with lng dev, it builds but the app gives you an error on the console (browser or terminal, doesn't care because it's the same, the postinstall forgot to copy the lightning.js library files because the cp fails before)

[2020-10-30T08:07:17.978Z]  "GET /lib/lightning.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
[2020-10-30T08:07:17.980Z]  "GET /lib/lightning.js" Error (404): "Not found"

In fact, you got the two libraries on the node_modules, the wpe-lightning-sdk and the @lightningjs/core (but not the @lightningjs/sdk and on the package.json you have the old reference github:rdkcentral/Lightning-SDK

By the way: node v14.2.0 npm v6.14.8 (lts)

jhderojasUVa commented 4 years ago

More clues:

If you add on the package.json both dependencies:

"wpe-lightning-sdk": "github:rdkcentral/Lightning-SDK",
"@lightningjs/sdk": "^3.0.0"

The wpe-lightning-sdk postinstall script will fail but the @lightningjs/sdk postinstall script will work.

Because of this, I found a manual solution:

Step 1. Put both dependencies on your package.json

"dependencies": {
    "wpe-lightning-sdk": "github:rdkcentral/Lightning-SDK",
    "@lightningjs/sdk": "^3.0.0"
  }

Step 2. Do an npm i. One will give you error but because the script will finish you don't have to be worried.

Step 3. Remove the old dependency wpe-lightning-sdk from your package.json

The problems seems to be that on the postinstall.js you think that you are at @lightningjs/sdk but it also executed on wpe-lightning-sdk (diferent directory and structure) getting you the error.

michielvandergeest commented 4 years ago

Just a quick update to confirm that indeed there seem to be some issues with the post install script in some cases.

We're working on an update, hopefully to be released early next week.

In the mean time, it's best to follow these steps to upgrade from v2 to v3:

npm install @lightningjs/sdk
npm uninstall wpe-lightning-sdk
michielvandergeest commented 4 years ago

Addressed in version 3.1.1 (by adding a warning with manual instructions to upgrade). Closing the ticket.