apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

Unable to resolve modules when used in Angular project #265

Closed davidgeary closed 3 years ago

davidgeary commented 3 years ago

Expected Behavior

I expect the project to build without errors.

Current Behavior

I'm writing an Angular project to which I've added nano (as per https://github.com/apache/couchdb-nano#typescript). But as soon as I include a call such as:

let n = Nano('http://localhost:5984');

I get errors when building the project about missing modules, eg:

Error: ./node_modules/nano/lib/nano.js Module not found: Error: Can't resolve 'http' in 'C:\code\my-app\node_modules\nano\lib'

(The actual error then lists all of the places it has checked for the existence of the missing module, which I'm omitting for brevity.)

The same error message is repeated for the https and stream modules.

Steps to Reproduce

Stackblitz

I've created a sample of this on Stackblitz. Stackblitz doesn't show the full underlying console error message unfortunately, but does show the message:

Can't find packages: url http https events punycode

Locally

If you have the Angular CLI installed, this can be seen in a minimal sample:

Create a new Angular app and then install nano:

ng new my-app --minimal=true --routing=false --skipGit=true --interactive=false
cd my-app
npm install nano

Edit tsconfig.json to include "allowSyntheticDefaultImports": true under compilerOptions.

Edit app.component.ts to:

Then build the project with ng build

Your Environment

glynnbird commented 3 years ago

I think this is as a result of using Nano in a browser, rather than server-side from within a Node.js project. So the browser doesn't know what the http, https and stream modules are.

I'm no expert in transpiling Node.js modules for browser use.

If I were writing to CouchDB from within the browser, I'd use Fetch API - not quite as easy as Nano, but definitely not unmanageable - and you get to learn the CouchDB API too! :)

davidgeary commented 3 years ago

That makes sense - thanks @glynnbird.

As it happens, I did end up writing directly against the CouchDB API (using Angular's HttpClient service), so yes, I've now learnt the API which is no bad thing.