SAP-archive / sap-devtoberfest-2020

The home of Devtoberfest 2020 - an open celebration of what makes us developers – coding and collaboration!
Apache License 2.0
180 stars 86 forks source link

CAP ex.4 - Error "Cannot find module 'sqlite3'" #81

Closed dasast closed 3 years ago

dasast commented 3 years ago

Hello, I am trying to finish ex. 4 of SAP Cloud Application Programming Model (CAP).

But whenever I try to click on "Books" at service web page I get an error.

obrazek

The error in Business Application Studio is

GET /catalog/Books [2020-11-21T20:08:46.513Z | ERROR | 1557126]: { Error: Cannot find module 'sqlite3' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at _loadSqlite (/extbin/npm/globals/lib/node_modules/@sap/cds-runtime/lib/sqlite/Service.js:9:15) at Promise (/extbin/npm/globals/lib/node_modules/@sap/cds-runtime/lib/sqlite/Service.js:31:20) at new Promise () at _new (/extbin/npm/globals/lib/node_modules/@sap/cds-runtime/lib/sqlite/Service.js:30:10) at SQLiteDatabase.acquire (/extbin/npm/globals/lib/node_modules/@sap/cds-runtime/lib/sqlite/Service.js:156:19) at SQLiteDatabase. (/extbin/npm/globals/lib/node_modules/@sap/cds-runtime/lib/sqlite/Service.js:110:29) code: 'MODULE_NOT_FOUND' }

I have installed sqlite and deployed db. npm install sqlite3 -D cds deploy --to sqlite:bookshop.db

I created db connection and connected to it without problem.

I think I followed instructions of this and previous exercises and I don't know what else to do.

Thank you!

maxstreifeneder commented 3 years ago

Hi @dasast,

could you please upload your project as a repo to GitHub so I can run it myself?

Cheers, Max

dasast commented 3 years ago

Hi,

I have decided to try lessons one more time. I deleted my dev space and created new one. But this time I wasn't successful even with deploying.

npm install sqlite3 -D ok cds deploy --to sqlite:bookshop.db ended with error

obrazek

I created repo https://github.com/dasast/cap_sqlite_error1 with tar.gz file with downloaded dev space. But is almost an initial one.

Thanks for help.

maxstreifeneder commented 3 years ago

HI @dasast,

the repository is unfortunately completely empty.

Regards, Max

dasast commented 3 years ago

Sorry, I probably forgot to commit it. Now it should be OK. Dasa

maxstreifeneder commented 3 years ago

Hm, that's interesting.

I tried npm install followed by npm start and it works for me - can you please try that once again as well?

dasast commented 3 years ago

Well, yes npm install followed by npm start works, but that is not where the problem is. Until the database is deployed, "Books" link returns error "No database connection". So I need to use database.

npm install followed by cds deploy still doesn't work.

And when I earlier was successful with cds deploy, the "Books" link returned error "MODULE_NOT_FOUND'

qmacro commented 3 years ago

Good morning @maxstreifeneder and @dasast - please permit me to join this discussion.

I am guessing from the screenshots you shared, @dasast , that you're doing this in the SAP Business Application Studio - is this correct? Assuming it is, I just tried it, with your tar and gzipped file in that repo you created, and it worked fine. Here's the output of a session just now:

user: user $ cd projects/ 
user: projects $ curl -L https://github.com/dasast/cap_sqlite_error1/raw/main/workspaces-ws-cl4f7.tar.gz | tar xzf -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   156  100   156    0     0   5571      0 --:--:-- --:--:-- --:--:--  5571
100 6231k  100 6231k    0     0  3458k      0  0:00:01  0:00:01 --:--:-- 7297k
user: projects $ ls
bookshop
user: projects $ cd bookshop/
user: bookshop $ npm install
npm WARN lifecycle The node binary used for scripts is /extbin/bin/node but npm is using /opt/nodejs/node-v10.22.1-linux-x64/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> @sap-cloud-sdk/core@1.32.1 postinstall /home/user/projects/bookshop/node_modules/@sap-cloud-sdk/core
> node usage-analytics.js

added 137 packages from 106 contributors and audited 257 packages in 4.074s

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

found 0 vulnerabilities

user: bookshop $ cds deploy --to sqlite:bookshop.db
/> successfully deployed to ./bookshop.db

/> updated ./package.json
user: bookshop $ npm start
npm WARN lifecycle The node binary used for scripts is /extbin/bin/node but npm is using /opt/nodejs/node-v10.22.1-linux-x64/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> bookshop@1.0.0 start /home/user/projects/bookshop
> npx cds run

[cds] - model loaded from 2 file(s):

  db/schema.cds
  srv/catalog-service.cds

[cds] - connect to db > sqlite { database: 'bookshop.db' }
[cds] - serving CatalogService { at: '/catalog' }

[cds] - launched in: 557.845ms
[cds] - server listening on { url: 'http://localhost:4004' }
[ terminate with ^C ]

and here's the successful retrieval of (no) books:

user: user $ curl http://localhost:4004/catalog/Books
{"@odata.context":"$metadata#Books","value":[]}
user: user $ 

I am therefore wondering whether you have the right tools installed into your Dev Space - did you start with the "SAP Cloud Business Application" type of Dev Space when you create it? Can you please try this flow (above) and let us know what happens?

dasast commented 3 years ago

Hello @qmacro, you are right, I am working in the SAP Business Application Studio as described in Devtoberfest2020 Week 3 SAP Cloud Application Programming Model (CAP).

I am using my trial account, dev space of type SAP Cloud Business Application. obrazek

I have tried it and I was successful !

The main reason is that you use command npm install which probably installs more packages than in the video SAP Cloud Application Programming Model - Ex. 03 - Associations & adding persistence used command npm install sqlite3 -D. After npm install I was already successful with command cds deploy --to sqlite:bookshop.db

And also the "Books" link already works.

Thanks very much, I am very happy than I can continue with CAP lessons.

Dasa

qmacro commented 3 years ago

Hey Dasa, thanks, that's great news! Happy hacking!

vikjdk7 commented 3 years ago

For me unsetting node path before running CDS deploy worked

run "npm install sqlite3 -D" run "unset NODE_PATH" run "cds deploy --to sqlite"