Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ReactNativeManualDevicePlugin - Cannot find module @microsoft/applicationinsights-react-native/dist-esm/manualIndex #46

Open
yoannHertienne opened this issue Apr 30, 2024 · 13 comments
Assignees
Labels
investigation required Further investigation or discussions required keep Do not Mark as Stale and close

Comments

@yoannHertienne
Copy link

yoannHertienne commented Apr 30, 2024

Description/Screenshot

Following your documentation, I'm using Expo, so I import ReactNativeManualDevicePlugin using
import { ReactNativeManualDevicePlugin } from "@microsoft/applicationinsights-react-native/dist-esm/manualIndex"

but then I'm getting the following Typescript error
image
image

Steps to Reproduce

  • OS/Browser: MacOS
  • React-Native Version: 0.73.6
  • SDK Version [e.g. 22]:
    "@microsoft/applicationinsights-react-native": "^4.1.1",
    "@microsoft/applicationinsights-web": "^3.2.0",
  • How you initialized the SDK:
    `import { ApplicationInsights, IApplicationInsights } from "@microsoft/applicationinsights-web"
    import { ReactNativeManualDevicePlugin } from "@microsoft/applicationinsights-react-native/dist-esm/manualIndex"
    import { myDeviceInfoModule } from "../../config/appInsight-device.ts"

// MS ApplicationInsights
const RNMPlugin = new ReactNativeManualDevicePlugin()
RNMPlugin.setDeviceInfoModule(myDeviceInfoModule)
const appInsight = new ApplicationInsights({
config: {
connectionString: Config.APPINSIGHT_CONNECTION_STRING,
extensions: [RNMPlugin],
},
})
appInsight.loadAppInsights()`

Expected behavior

Typescript should detect the module. I have the same issue with my Unit Test using Jest

Additional context
Add any other context about the problem here.

@yoannHertienne yoannHertienne changed the title [BUG] [BUG] ReactNativeManualDevicePlugin - Cannot find module @microsoft/applicationinsights-react-native/dist-esm/manualIndex Apr 30, 2024
@MSNev
Copy link
Contributor

MSNev commented Apr 30, 2024

I have seen this at times, and documented as is a hack because not all build environment support the new entry points.

I suspect that if you throw a ".js" onto the end of the import that it (should) work as the file exists but the version of TypeScript you are using (or the mode its running in) doesn't like / infer the import type.

@yoannHertienne
Copy link
Author

yoannHertienne commented May 7, 2024

@MSNev Adding ".js" didn't fix the issue.

When I'm trying to use ReactNativePlugin, for a non-expo app, I'm getting quite similar issue:

image

Are you sure of your package.json or typing ?

@Karlie-777
Copy link
Contributor

maybe try:

  1. add a new declaration file example.d.ts with declare module 'reactNativePlugin';
    add your example.d.ts to tsconfig.json
  {
    "compilerOptions": {
...
      },
      "include": [
        ...
        "example.d.ts"
      ],
   
    }
  1. maybe try set allowJs: true in your tsconfig.json

@luanlcampos
Copy link

I am getting the same issue as @yoannHertienne described. Any news on that?

@yoannHertienne
Copy link
Author

@Karlie-777 I tried your solution, but I was not able to fix it, still typescript error to import your module.

@Karlie-777
Copy link
Contributor

@siyuniu-ms it seems that all manual device interfaces

export { ReactNativeManualDevicePlugin, INativeDevice, IReactNativePluginConfig, IDeviceInfoModule };
are not exported from index.ts https://github.com/microsoft/applicationinsights-react-native/blob/main/applicationinsights-react-native/src/index.ts

@siyuniu-ms
Copy link
Contributor

Hi, @yoannHertienne @luanlcampos Could you check whether the provided sample app https://github.com/microsoft/applicationinsights-react-native/tree/main/sample works for you?
I changed the app locally to match the version as yours
"@microsoft/applicationinsights-react-native": "^4.1.1", "@microsoft/applicationinsights-web": "^3.2.0",
and also added
var RNPlugin = new ReactNativeManualDevicePlugin(); RNPlugin.setDeviceInfoModule(myDeviceInfoModule);
It works good on my windows machine.
image

@siyuniu-ms
Copy link
Contributor

Could you also check the typescript version in the node_modules folder? Based on the conversation in https://stackoverflow.com/questions/70296652/how-can-i-use-exports-in-package-json-for-nested-submodules-and-typescript, typescript that has version lower than 4.7 didn't support export (which may cause the issue of not finding the right type files.)

@yoannHertienne
Copy link
Author

Could you also check the typescript version in the node_modules folder? Based on the conversation in https://stackoverflow.com/questions/70296652/how-can-i-use-exports-in-package-json-for-nested-submodules-and-typescript, typescript that has version lower than 4.7 didn't support export (which may cause the issue of not finding the right type files.)

I'm using "typescript": "5.4.5".

@yoannHertienne
Copy link
Author

Hi, @yoannHertienne @luanlcampos Could you check whether the provided sample app https://github.com/microsoft/applicationinsights-react-native/tree/main/sample works for you? I changed the app locally to match the version as yours "@microsoft/applicationinsights-react-native": "^4.1.1", "@microsoft/applicationinsights-web": "^3.2.0", and also added var RNPlugin = new ReactNativeManualDevicePlugin(); RNPlugin.setDeviceInfoModule(myDeviceInfoModule); It works good on my windows machine. image

I tried your sample, and after installing Eslint and typescript, import is not giving any error.

I will create new project from scratch and import your library.

@yoannHertienne
Copy link
Author

yoannHertienne commented Aug 20, 2024

@Karlie-777 @siyuniu-ms I tested the sample project again and with typescript, it's not working and give me the same error.
Step to reproduce

  1. Clone the sample project
  2. Install typescript for React Native following guide https://reactnative.dev/docs/typescript
    a.npm install -D @tsconfig/react-native @types/jest @types/react @types/react-test-renderer typescript
    b. Create tsconfig.json file at the root of the project and add the following inside it:
    { "extends": "@tsconfig/react-native/tsconfig.json" }

Then I'm getting
image

So it's working with Javascript, but not with Typescript

@augusthjerrild
Copy link

Hi!

Is there any news about this? :-)

I get the same issue in my Expo app. It seems like the type definition (manualIndex.d.ts) is not working when importing since it's implicitly has an 'any' type.

I'm importing with import {ReactNativeManualDevicePlugin} from '@microsoft/applicationinsights-react-native/dist-esm/manualIndex';

@SomniumDigital
Copy link

SomniumDigital commented Nov 15, 2024

We've experienced this exact same problem when running our tests using Jest 29 simply importing the below:

import { ReactNativePlugin } from '@microsoft/applicationinsights-react-native';

const RNPlugin = new ReactNativePlugin();

After lots of trial and error I found that the last working version was the combination below:

"@microsoft/applicationinsights-react-native": "4.2.0",
"@microsoft/applicationinsights-web": "3.2.0",

Clearly something was introduced after this (4.2.0?) that has likely caused the issues we've been experiencing.

Update:
I believe it's the changes in this PR which have caused the issue.

@MSNev MSNev added investigation required Further investigation or discussions required keep Do not Mark as Stale and close labels Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigation required Further investigation or discussions required keep Do not Mark as Stale and close
Projects
None yet
Development

No branches or pull requests

7 participants