1

I am having difficulty importing the @google-cloud/logging npm package in my React Native project. I tried with npm install @google-cloud/logging and yarn add @google-cloud/logging but when I try to use it in my code via const {Logging} = require('@google-cloud/logging'); or import {Logging} from '@google-cloud/logging'; commands I am getting a stream not found exception.

Exception:

error: Error: Unable to resolve module stream from 
C:\Users\User\Documents\repos\project\node_modules\@google-cloud\logging\build\src\index.js: 
stream could not be found within the project.

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
  26 | // eslint-disable-next-line @typescript-eslint/no-var-requires
  27 | const pumpify = require('pumpify');
> 28 | const streamEvents = require("stream-events");
     |                               ^
  29 | const middleware = require("./middleware");
  30 | exports.middleware = middleware;
  31 | const metadata_1 = require("./utils/metadata");

at ModuleResolver.resolveDependency (C:\Users\User\Documents\repos\project\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:191:15)
    at DependencyGraph.resolveDependency (C:\Users\User\Documents\repos\project\node_modules\metro\src\node-haste\DependencyGraph.js:353:43)
    at Object.resolve (C:\Users\User\Documents\repos\project\node_modules\metro\src\lib\transformHelpers.js:271:42)
    at resolve (C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:571:33)
    at C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:587:26
    at Array.reduce (<anonymous>)
    at resolveDependencies (C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:586:33)
    at C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:275:33
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:87:24)

I tried deleting node_modules folder from the project and installing the packages again, resetting the Metro cache. The error still persists.

I have "react-native": "~0.63.4" and "@google-cloud/logging": "^9.6.1" in my package.json

Is there a way to implement Google Cloud Logging in a React Native project?

UPDATE 1

I resolved the stream exception with yarn add stream command. Now it asks for the fs package which I added with yarn but now I receive the following error:

Error: While trying to resolve module `fs` from file `C:\Users\User\Documents\repos\project\node_modules\@google-cloud\logging\build\src\utils\metadata.js`, the package `C:\Users\User\Documents\repos\project\node_modules\fs\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\Users\User\Documents\repos\project\node_modules\fs\index.js`. Indeed, none of these files exist:

  * C:\Users\User\Documents\repos\project\node_modules\fs\index.js(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  * C:\Users\User\Documents\repos\project\node_modules\fs\index.js\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
    at DependencyGraph.resolveDependency (C:\Users\User\Documents\repos\project\node_modules\metro\src\node-haste\DependencyGraph.js:376:17)
    at Object.resolve (C:\Users\User\Documents\repos\project\node_modules\metro\src\lib\transformHelpers.js:271:42)
ndencies.js:571:33)
    at C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:587:26
    at Array.reduce (<anonymous>)
    at resolveDependencies (C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:586:33)
    at C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:275:33
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:87:24)
    at _next (C:\Users\User\Documents\repos\project\node_modules\metro\src\DeltaBundler\traverseDependencies.js:107:9)
Asp1re
  • 353
  • 1
  • 12
  • Did you include other extensions into the `metro.config.js` as was mentioned in [this thread](https://stackoverflow.com/questions/60124435/however-this-package-itself-specifies-a-main-module-field-that-could-not-be-r)? – Farid Shumbar Oct 07 '21 at 09:12
  • @FaridShumbar yes, issue still persists. Now I get a new exception that the "fs" library cannot be resolved. Similar to the one above with my "stream" error. – Asp1re Oct 07 '21 at 09:33
  • I guess it can be solved by installing [`react-native-fs`](https://github.com/itinance/react-native-fs) and updating the requirements from `fs` to `react-native-fs` – Farid Shumbar Oct 07 '21 at 09:46

1 Answers1

0

To wrap it up, @Asp1re has found a solution to solve the initial Error: Unable to resolve module stream which is running the yarn add stream command.

After that, another error occurred:

Error: While trying to resolve module `fs` from file `C:\Users\User\Documents\repos\project\node_modules\@google-cloud\logging\build\src\utils\metadata.js`, the package `C:\Users\User\Documents\repos\project\node_modules\fs\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\Users\User\Documents\repos\project\node_modules\fs\index.js`. Indeed, none of these files exist

This should be solved by adding other extensions into the metro.config.js:

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  resolver: {
    sourceExts: ['jsx', 'js', 'ts', 'tsx'], //add here
  },
};

and installing the react-native-fs. Make sure to update the requirements from fs to react-native-fs.

Farid Shumbar
  • 1,360
  • 3
  • 10