aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Unable to resolve module `crypto` from `node_modules/crypto-js/core.js`: crypto could not be found within the project #8341

Closed vikasaroy closed 3 years ago

vikasaroy commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication, Analytics

Amplify Categories

analytics

Environment information

``` # Put output below this line System: OS: macOS 11.3.1 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 230.74 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 12.18.3 - /usr/local/bin/node npm: 6.14.6 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Browsers: Chrome: 90.0.4430.212 Firefox: 88.0.1 Safari: 14.1 npmPackages: @babel/core: ^7.8.4 => 7.12.10 @babel/runtime: ^7.8.4 => 7.12.5 @react-native-async-storage/async-storage: ^1.13.4 => 1.13.4 @react-native-community/eslint-config: ^1.1.0 => 1.1.0 @react-native-community/masked-view: ^0.1.10 => 0.1.10 @react-native-community/netinfo: ^6.0.0 => 6.0.0 @react-native-community/slider: ^3.0.3 => 3.0.3 @react-native-community/viewpager: ^4.2.3 => 4.2.3 @react-navigation/bottom-tabs: ^5.11.3 => 5.11.3 @react-navigation/native: ^5.8.10 => 5.8.10 (3.8.3) @react-navigation/stack: ^5.12.8 => 5.12.8 @types/jest: ^26.0.20 => 26.0.20 @types/react: ^17.0.1 => 17.0.1 @types/react-native: ^0.63.48 => 0.63.48 @types/react-test-renderer: ^17.0.0 => 17.0.0 HelloWorld: 0.0.1 amazon-cognito-identity-js: ^5.0.1 => 5.0.1 appcenter: 4.1.0 => 4.1.0 appcenter-analytics: 4.1.0 => 4.1.0 appcenter-crashes: 4.1.0 => 4.1.0 aws-amplify: ^4.0.2 => 4.0.2 aws-amplify-react-native: ^5.0.0 => 5.0.0 axios: ^0.21.1 => 0.21.1 babel-jest: ^25.1.0 => 25.5.1 babel-plugin-module-resolver: ^4.1.0 => 4.1.0 date-fns: ^2.21.1 => 2.21.1 eslint: ^6.5.1 => 6.8.0 eslint-import-resolver-babel-module: ^5.2.0 => 5.2.0 eslint-plugin-import: ^2.22.1 => 2.22.1 hermes-inspector-msggen: 1.0.0 jest: ^25.1.0 => 25.5.4 lodash: ^4.17.21 => 4.17.21 memo-parser: 0.2.1 metro-react-native-babel-preset: ^0.59.0 => 0.59.0 (0.58.0) react: 16.13.1 => 16.13.1 react-animated: 0.1.0 react-native: 0.63.4 => 0.63.4 react-native-bouncy-checkbox: ^1.0.8 => 1.0.8 react-native-dash: 0.0.11 => 0.0.11 react-native-geolocation-service: ^5.2.0 => 5.2.0 react-native-gesture-handler: ^1.9.0 => 1.9.0 react-native-linear-gradient: ^2.5.6 => 2.5.6 react-native-loading-spinner-overlay: ^2.0.0 => 2.0.0 react-native-maps: 0.27.1 => 0.27.1 react-native-open-maps: ^0.3.6 => 0.3.6 react-native-reanimated: ^1.13.2 => 1.13.2 react-native-render-html: ^5.1.0 => 5.1.0 react-native-safe-area-context: ^3.1.9 => 3.1.9 react-native-screens: ^2.16.1 => 2.16.1 react-native-svg: ^12.1.0 => 12.1.0 react-navigation: ^4.4.3 => 4.4.3 react-navigation-stack: ^1.10.3 => 1.10.3 react-test-renderer: 16.13.1 => 16.13.1 typescript: ^4.1.4 => 4.1.4 npmGlobalPackages: @aws-amplify/cli: 4.29.2 aws-cdk: 1.105.0 docusaurus-init: 1.14.1 gatsby-cli: 2.16.2 http-server: 0.12.3 jest-cli: 26.6.3 lumigo-cli: 0.48.3 npm: 6.14.6 serverless: 1.83.3 ```

Describe the bug

I want to integrate AWS Pinpoint in my application built in react-native. When I add AWS Amplify to my pre-existing react-native project, it is giving me following error:

error: Error: Unable to resolve module `crypto` from `node_modules/crypto-js/core.js`: crypto 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: rm -rf node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
    at ModuleResolver.resolveDependency (/Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:186:15)
    at ResolutionRequest.resolveDependency (/Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:52:18)
    at DependencyGraph.resolveDependency (/Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/node-haste/DependencyGraph.js:287:16)
    at Object.resolve (/Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/lib/transformHelpers.js:267:42)
    at /Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:434:31
    at Array.map (<anonymous>)
    at resolveDependencies (/Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:431:18)
    at /Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:275:33
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/vikasroy/Desktop/code/mcCustomerApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:87:24)
[Wed May 26 2021 02:44:37.730]  ERROR    [Error: undefined Unable to resolve module `crypto` from `node_modules/crypto-js/core.js`: crypto 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: rm -rf node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*]

Expected behavior

It should have discovered the crypto package as it is present in node_module.

Reproduction steps

  1. Install amplify js dependency using below command
npm install aws-amplify aws-amplify-react-native amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage
  1. Initialize Amplify by adding below code to your index.js
import Amplify from 'aws-amplify';

Amplify.configure({
  // To get the AWS Credentials, you need to configure
  // the Auth module with your Cognito Federated Identity Pool
  Auth: {
    identityPoolId: 'us-east-1:xxx-xxx-xxx-xxx-xxx',
    region: 'us-east-1'
  },
  Analytics: {
    // OPTIONAL - disable Analytics if true
    disabled: false,
    // OPTIONAL - Allow recording session events. Default is true.
    autoSessionRecord: true,

    AWSPinpoint: {
      // OPTIONAL -  Amazon Pinpoint App Client ID
      appId: 'XXXXXXXXXXabcdefghij1234567890ab',
      // OPTIONAL -  Amazon service region
      region: 'XX-XXXX-X',
      // OPTIONAL -  Customized endpoint
      endpointId: 'XXXXXXXXXXXX',
      // OPTIONAL - Default Endpoint Information
      endpoint: {
        address: 'xxxxxxx', // The unique identifier for the recipient. For example, an address could be a device token, email address, or mobile phone number.
        attributes: {
          // Custom attributes that your app reports to Amazon Pinpoint. You can use these attributes as selection criteria when you create a segment.
          hobbies: ['piano', 'hiking'],
        },
        channelType: 'APNS', // The channel type. Valid values: APNS, GCM
        demographic: {
          appVersion: 'xxxxxxx', // The version of the application associated with the endpoint.
          locale: 'xxxxxx', // The endpoint locale in the following format: The ISO 639-1 alpha-2 code, followed by an underscore, followed by an ISO 3166-1 alpha-2 value
          make: 'xxxxxx', // The manufacturer of the endpoint device, such as Apple or Samsung.
          model: 'xxxxxx', // The model name or number of the endpoint device, such as iPhone.
          modelVersion: 'xxxxxx', // The model version of the endpoint device.
          platform: 'xxxxxx', // The platform of the endpoint device, such as iOS or Android.
          platformVersion: 'xxxxxx', // The platform version of the endpoint device.
          timezone: 'xxxxxx' // The timezone of the endpoint. Specified as a tz database value, such as Americas/Los_Angeles.
        },
        location: {
          city: 'xxxxxx', // The city where the endpoint is located.
          country: 'xxxxxx', // The two-letter code for the country or region of the endpoint. Specified as an ISO 3166-1 alpha-2 code, such as "US" for the United States.
          latitude: 0, // The latitude of the endpoint location, rounded to one decimal place.
          longitude: 0, // The longitude of the endpoint location, rounded to one decimal place.
          postalCode: 'xxxxxx', // The postal code or zip code of the endpoint.
          region: 'xxxxxx' // The region of the endpoint location. For example, in the United States, this corresponds to a state.
        },
        metrics: {
          // Custom metrics that your app reports to Amazon Pinpoint.
        },
        /** Indicates whether a user has opted out of receiving messages with one of the following values:
         * ALL - User has opted out of all messages.
         * NONE - Users has not opted out and receives all messages.
         */
        optOut: 'ALL',
        // Customized userId
        userId: 'XXXXXXXXXXXX',
        // User attributes
        userAttributes: {
          interests: ['football', 'basketball', 'AWS']
          // ...
        }
      },

      // Buffer settings used for reporting analytics events.
      // OPTIONAL - The buffer size for events in number of items.
      bufferSize: 1000,

      // OPTIONAL - The interval in milliseconds to perform a buffer check and flush if necessary.
      flushInterval: 5000, // 5s

      // OPTIONAL - The number of events to be deleted from the buffer when flushed.
      flushSize: 100,

      // OPTIONAL - The limit for failed recording retries.
      resendLimit: 5
    }
  }
});

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

iartemiev commented 3 years ago

Hey @roysailor, I just followed your repro steps with a fresh RN CLI app using NPM@6 to install the dependencies (like you have) and was not able to reproduce that module error. I made sure that Amplify Analytics is working as expected too and is able to send events as expected.

I would suggest doing a fresh re-install of your dependencies:

  1. rm -rf node_modules package-lock.json
  2. npm i
  3. npx pod-install

Please let us know if you're still getting that error afterward.

vikasaroy commented 3 years ago

Hey @roysailor, I just followed your repro steps with a fresh RN CLI app using NPM@6 to install the dependencies (like you have) and was not able to reproduce that module error. I made sure that Amplify Analytics is working as expected too and is able to send events as expected.

I would suggest doing a fresh re-install of your dependencies:

1. `rm -rf node_modules package-lock.json`

2. `npm i`

3. `npx pod-install`

Please let us know if you're still getting that error afterward.

This worked for me. Thanks a lot.

cross19xx commented 3 years ago

It's worth noting that if you're running for android, clean the project first

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.