简体   繁体   中英

Create react native module and use it in application

I want to create react-native module for iOS and Android and use it in an application.

I want to use it like a local module, not uploaded to npm.

When i follow the steps described in the official documentation i always have those error on the import of my module into App.js file :

import MyModule from 'react-test-module';

Could not find a declaration file for module 'react-test-module'. '/Users/dbellerose/Path/to/My/Module/react-test-module/index.js' implicitly has an 'any' type. Try npm install @types/react-test-modulet if it exists or add a new declaration (.d.ts) file containing `declare module 'react-test-module';

When i run the iOS application :

Unable to resolve module react-test-module from /Users/Path/To/My/App/App.js : Module react-test-module does not exist in the Haste module map.

The message give me some tips to solve the problem, but it does not work.

To resolve try the following: 1. Clear watchman watches: watchman watch-del-all . 2. Delete the node_modules folder: rm -rf node_modules && npm install . 3. Reset Metro Bundler cache: rm -rf /tmp/metro-bundler-cache-* or npm start -- --reset-cache . 4. Remove haste cache: rm -rf /tmp/haste-map-react-native-packager-* .

So what do you know what is the good process to create a module and use it in application without uploading the package to npm ?

Best regards,

David

Here's some steps I could share with you as a checklist:

  1. yarn global add create-react-native-module
  2. create-react-native-module MyLibrary
  3. Cd to MyLibrary folder, run yarn install
  4. Work on your module

To link to your project, you will need to add the following to your package.json (the path should be wherever your module is sitting at)

"react-native-test-module": "file:../", 
"react-native-my-library": "file:../../react-native-my-library/"

** Do keep in mind, if you have new methods appended in module. You will need to:

  1. Remove node_modules and Pods from your project
  2. Re-execute yarn/npm install & pod install

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM