question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Source Javascript files required from outside of root directory -- are not found

See original GitHub issue

This is a question on how to configure metro.config.js to allow to include source files

My index.android.js has

import { functionX} from './../rn.common/src/js.app/SomeFunctionsThatUseRN'

The above, unfortunately causes an error.

My metro.config.js

const path = require('path');

let allModules =new Proxy({}, {
      get: (target, name) => path.join(process.cwd(), `node_modules/${name}`),
});

allModules['rnjs_common']=path.resolve(__dirname,"../rn.common/src/js.app");

module.exports = {
    resolver: {
    extraNodeModules: allModules
  },    
  projectRoot: path.resolve(__dirname),
  watchFolders: [
    path.resolve(__dirname, "../rn.common/src/js.app"), 
  ]
};

The question is very similar to this open issue https://github.com/facebook/metro/issues/7#issuecomment-508129053 But in there folks are trying to add custom node_modules from a non-root folder. And in my case I need to be able to include non node_modules source file.

It seems that some form of an include option is missing from the resolver config in Metro Is that possible to do with current version of Metro ?

Previously, it seemed it was possible by providing multiple directories to ‘getProjectRoot’ (as noted here ) in rn-cli.config.js But that’s no longer supported, and I could not find any alternatives/migrations.

Thank you Environment

Metro: 0.54.1 React Native: 0.60.5 System: OS: Windows 10 Binaries: Node: 12.8.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.6.0 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.5.0 - C:\Program Files\nodejs\npm.CMD

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
yeegrcommented, Oct 24, 2019

I’m on RN 0.61.2, still unable to include files outside of the project folder. I’ve tried the following edits with metro.config.js, neither one works

module.exports = {
  projectRoot: './',
  watchFolders: [
    '../common'
  ]
}
module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  resolver: {
    sourceExts: ['ts', 'tsx', 'js']
  },
  projectRoot: path.resolve(__dirname),
  watchFolders: [
    path.resolve(__dirname, '../common')
  ]
}

this completely breaks my folder structure, as /common houses constants and pure functions that could be reused by both the server and client.

need this fixed soon.

1reaction
mamazcommented, Oct 16, 2019

I have the same issue importing image assets on Android outside root folder, it works on iOS though.

RN: 0.59.9

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I link JS external files outside of my PHP project root ...
Your JavaScript files have to be accessible to the browser because they are executed by the browser and not by the server.
Read more >
How to import files from outside of root directory with React ...
A simple solution could be to create a symlink inside the root directory but metro bundler doesn't support symlink. So we're going to...
Read more >
Documentation - Module Resolution - TypeScript
Ask the file named /root/src/moduleB.js , if it exists. Ask the folder /root/src/moduleB if it contains a file named package.
Read more >
jsconfig.json Reference - Visual Studio Code
The presence of jsconfig.json file in a directory indicates that the directory is the root of a JavaScript Project. The jsconfig.json file specifies...
Read more >
Find the Document Root of your Website - InMotion Hosting
If no particular file is specified, the directory index file will be the first file the server loads from a folder. For example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found