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

Package requires Node Module 'fs', which is not part of RN #96

Closed
LogicAnalysis opened this issue Nov 5, 2020 · 7 comments
Closed

Package requires Node Module 'fs', which is not part of RN #96

LogicAnalysis opened this issue Nov 5, 2020 · 7 comments

Comments

@LogicAnalysis
Copy link

Am I missing something? The first line of code in this package is to require 'fs', which is not even part of React Native. Is this only meant to run on Node? How would it run on a physical device?

Anyways, here's the error code:

"The package at "node_modules/react-native-dotenv/index.js" attempted to import the Node standard library module "fs"."

@Gerlison
Copy link

Gerlison commented Nov 9, 2020

This is because you have to use import {KEY} from "@env"; instead from "react-native-dotenv"

@kalleriakronos24
Copy link

try :

  1. install package react-native-fs as native filesystem for react-native, because using fs u cannot read/doing anything with the native filesystem(?) idk, try install react-native-fs module
  2. follow the installation guide in react-native-fs's github
  3. lookup into the react-native-dotenv's index.js rename const { readFileSync } = require('fs') to const { readFile } = require('react-native-fs'); also rename the invoked function readFileSync in line 8 to readFile

this is extra step of above steps :

*you'll also getting error of dotenv package as well as u fixed above

try :

  1. lookup into dotenv package in your nodemodules, open folder lib, change the fs import to react-native-fs and in line 96 rename readFileSync or something to readFile

umm maybe this also a extra step, because u'll also facing error from following the react-native-fs installation guide

if u got error RNFSManager trying to override something something then don't add this package to ReactPackageList
u can just import the package in MainApplication.java import com.rnfs.RNFSPackage; just that, dont add the RNFSPackage to the ReactPackageList

this is only will fixed the error, i still could not use the react-native-dotenv package

.env
SERVER_URL=SECRET

main.js

import { SERVER_URL } from 'react-native-dotenv';

console.log(SERVER_URL) // returns undefined
``
any inputs would be appreciated, lets fix this issue and able to use the package properly

@kalleriakronos24
Copy link

kalleriakronos24 commented Nov 9, 2020

oh, as this guy comment #52 (comment)

i was put this configuration in .babelrc

{
    "plugins": [
      ["module:react-native-dotenv", {
        "moduleName": "@env",
        "path": ".env",
        "blacklist": null,
        "whitelist": null,
        "safe": false,
        "allowUndefined": true
      }]
    ]
  }

after i followed the guy's comment and add that config to .babel-config.js

and it became like this

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  "plugins": [
    ["module:react-native-dotenv", {
      "moduleName": "@env",
      "path": ".env",
      "blacklist": null,
      "whitelist": null,
      "safe": true,
      "allowUndefined": true
    }]
  ]
};

and yea its all working perfectly now, cheers all

@faizrktm
Copy link

no need to have extra configuration, just import {KEY} from '@env'; and you good to go :D

@kalleriakronos24
Copy link

kalleriakronos24 commented Nov 12, 2020

no need to have extra configuration, just import {KEY} from '@env'; and you good to go :D

actually, really ? i mean i tried to only "just import {key} from '@env' and this line added in .babel-config

    ["module:react-native-dotenv", {
      "moduleName": "@env",
      "path": ".env",
      "blacklist": null,
      "whitelist": null,
      "safe": true,
      "allowUndefined": true
    }]
  ]

this error is still occured on me The package at "node_modules/react-native-dotenv/index.js" attempted to import the Node standard library module "fs".", i mean what've did in here my 1st comment plus my 2nd comment does the magic for me :D

@goatandsheep
Copy link
Owner

goatandsheep commented Nov 13, 2020

Check out the comments in the duplicate tickets #20 #32 #78

@goatandsheep
Copy link
Owner

@LogicAnalysis congratulations and thank you for your first ticket ever on Github! This issue has come up numerous times so I've added the solution to the troubleshooting page. If there are any other issues please feel free to open a new ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants