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

Field 'browser' doesn't contain a valid alias configuration #2095

Closed
fansilva1991 opened this issue Jul 26, 2021 · 10 comments
Closed

Field 'browser' doesn't contain a valid alias configuration #2095

fansilva1991 opened this issue Jul 26, 2021 · 10 comments

Comments

@fansilva1991
Copy link

The problem

While running:

webpack serve --mode development --config webpack.config.js

I'm getting the following errors:

ERROR in ./node_modules/react-native/Libraries/Network/XMLHttpRequest.js 1:1656-1682
Module not found: Error: Can't resolve './RCTNetworking' in '.../node_modules/react-native/Libraries/Network'
resolve './RCTNetworking' in '.../node_modules/react-native/Libraries/Network'
  using description file: .../node_modules/react-native/package.json (relative path: ./Libraries/Network)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: .../node_modules/react-native/package.json (relative path: ./Libraries/Network/RCTNetworking)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking doesn't exist
      .web.ts
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.ts doesn't exist
      .web.tsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.tsx doesn't exist
      .web.mjs
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.mjs doesn't exist
      .web.js
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.js doesn't exist
      .web.jsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.jsx doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.tsx doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.js doesn't exist
      .jsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.jsx doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.json doesn't exist
      .wasm
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.wasm doesn't exist
      as directory
        .../node_modules/react-native/Libraries/Network/RCTNetworking doesn't exist
 @ ./node_modules/react-native/Libraries/Network/XHRInterceptor.js 1:32-59
 @ ./node_modules/reactotron-react-native/dist/index.js 1:636-692
 @ ./config/ReactotronConfig.js 1:133-167
 @ ./screens/store.js 1:1367-1403
 @ ./index.web.js 1:397-423

webpack 5.46.0 compiled with 1 error and 1 warning in 8353 ms
ℹ 「wdm」: Failed to compile.
ℹ 「wdm」: Compiling...
✖ 「wdm」: assets by status 17.7 MiB [cached] 88 assets
assets by path . 7.04 KiB
  asset manifest.json 3.84 KiB [emitted]
  asset index.html 3.19 KiB [emitted]
cached modules 3.72 MiB (javascript) 7.07 KiB (runtime) [cached] 1133 modules
./screens/store.js 1.82 KiB [built]

WARNING in ./node_modules/react-native/Libraries/Performance/Systrace.js 1:3269-3276
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/react-native/Libraries/Utilities/createPerformanceLogger.js 1:529-563
 @ ./node_modules/react-native/Libraries/Utilities/GlobalPerformanceLogger.js 1:148-184
 @ ./node_modules/react-native/Libraries/Network/XMLHttpRequest.js 1:1590-1637
 @ ./node_modules/react-native/Libraries/Network/XHRInterceptor.js 1:32-59
 @ ./node_modules/reactotron-react-native/dist/index.js 1:636-692
 @ ./config/ReactotronConfig.js 1:133-167
 @ ./screens/store.js 1:1367-1403
 @ ./index.web.js 1:397-423

1 warning has detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

ERROR in ./node_modules/react-native/Libraries/Network/XMLHttpRequest.js 1:1656-1682
Module not found: Error: Can't resolve './RCTNetworking' in '.../node_modules/react-native/Libraries/Network'
resolve './RCTNetworking' in '.../node_modules/react-native/Libraries/Network'
  using description file: .../node_modules/react-native/package.json (relative path: ./Libraries/Network)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: .../node_modules/react-native/package.json (relative path: ./Libraries/Network/RCTNetworking)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking doesn't exist
      .web.ts
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.ts doesn't exist
      .web.tsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.tsx doesn't exist
      .web.mjs
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.mjs doesn't exist
      .web.js
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.js doesn't exist
      .web.jsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.jsx doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.tsx doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.js doesn't exist
      .jsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.jsx doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.json doesn't exist
      .wasm
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.wasm doesn't exist
      as directory
        .../node_modules/react-native/Libraries/Network/RCTNetworking doesn't exist
 @ ./node_modules/react-native/Libraries/Network/XHRInterceptor.js 1:32-59
 @ ./node_modules/reactotron-react-native/dist/index.js 1:636-692
 @ ./config/ReactotronConfig.js 1:133-167
 @ ./screens/store.js 1:1367-1403
 @ ./index.web.js 1:397-423

webpack 5.46.0 compiled with 1 error and 1 warning in 396 ms
ℹ 「wdm」: Failed to compile.
ℹ 「wdm」: Compiling...
✖ 「wdm」: assets by status 17.7 MiB [cached] 88 assets
assets by path . 7.04 KiB
  asset manifest.json 3.84 KiB [emitted]
  asset index.html 3.19 KiB [emitted]
cached modules 3.72 MiB (javascript) 7.07 KiB (runtime) [cached] 1133 modules
./screens/store.js 1.82 KiB [built]

WARNING in ./node_modules/react-native/Libraries/Performance/Systrace.js 1:3269-3276
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/react-native/Libraries/Utilities/createPerformanceLogger.js 1:529-563
 @ ./node_modules/react-native/Libraries/Utilities/GlobalPerformanceLogger.js 1:148-184
 @ ./node_modules/react-native/Libraries/Network/XMLHttpRequest.js 1:1590-1637
 @ ./node_modules/react-native/Libraries/Network/XHRInterceptor.js 1:32-59
 @ ./node_modules/reactotron-react-native/dist/index.js 1:636-692
 @ ./config/ReactotronConfig.js 1:133-167
 @ ./screens/store.js 1:1367-1403
 @ ./index.web.js 1:397-423

1 warning has detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

ERROR in ./node_modules/react-native/Libraries/Network/XMLHttpRequest.js 1:1656-1682
Module not found: Error: Can't resolve './RCTNetworking' in '.../node_modules/react-native/Libraries/Network'
resolve './RCTNetworking' in '.../node_modules/react-native/Libraries/Network'
  using description file: .../node_modules/react-native/package.json (relative path: ./Libraries/Network)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: .../node_modules/react-native/package.json (relative path: ./Libraries/Network/RCTNetworking)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking doesn't exist
      .web.ts
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.ts doesn't exist
      .web.tsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.tsx doesn't exist
      .web.mjs
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.mjs doesn't exist
      .web.js
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.js doesn't exist
      .web.jsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.web.jsx doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.tsx doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.js doesn't exist
      .jsx
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.jsx doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.json doesn't exist
      .wasm
        Field 'browser' doesn't contain a valid alias configuration
        .../node_modules/react-native/Libraries/Network/RCTNetworking.wasm doesn't exist
      as directory
        .../node_modules/react-native/Libraries/Network/RCTNetworking doesn't exist
 @ ./node_modules/react-native/Libraries/Network/XHRInterceptor.js 1:32-59
 @ ./node_modules/reactotron-react-native/dist/index.js 1:636-692
 @ ./config/ReactotronConfig.js 1:133-167
 @ ./screens/store.js 1:1367-1403
 @ ./index.web.js 1:397-423

Which don't happen if remove the imports for NetInfo and Reactotron , but it is not a valid solution for us because we need to use it in the web and mobile versions.

How to reproduce

Steps to reproduce:

  1. Run webpack serve --mode development --config webpack.config.js in your application, with Reactotron or NetInfo installed.

Expected behavior

The application should run without errors while importing Reactotron and NetInfo.

Environment (include versions). Did this work in previous versions?

  • React Native for Web (version): "react-native-web": "^0.15.6",
  • React (version): "react": "17.0.1",
  • React Native (version): "react-native": "0.64.2",
  • Reactotron (version): "reactotron-react-native": "^5.0.0",
  • NetInfo (version): "@react-native-community/netinfo": "^6.0.0",
  • Browser: Don't apply

webpack.config.js

const path = require('path');
const {DefinePlugin} = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const {WebpackManifestPlugin} = require('webpack-manifest-plugin');

const babelLoaderConfiguration = {
  test: /\.js$/,
  include: [
    __dirname,
    path.resolve(__dirname, 'node_modules/react-native-uncompiled'),
  ],
  use: {
    loader: 'babel-loader',
    options: {
      configFile: true,
      babelrc: false,
      cacheDirectory: true,
      presets: ['module:metro-react-native-babel-preset'],
      plugins: ['react-native-web'],
    },
  },
};

const htmlLoaderConfiguration = {
  test: /\.html$/,
  use: [
    {
      loader: 'html-loader',
    },
  ],
};

const fileLoaderConfiguration = {
  test: /\.(css|png|jpe?g|gif)$/i,
  use: [
    {
      loader: 'file-loader',
      options: {
        esModule: false,
      },
    },
  ],
};

module.exports = (env, argv) => {
  const mode = argv.mode;

  const devtool = mode === 'production' ? 'source-map' : 'eval-source-map';

  return {
    entry: [path.resolve(__dirname, 'index.web.js')],

    output: {
      publicPath: '/',
      path: path.join(path.resolve(__dirname), 'web'),
      // TODO: need to make this unique so browsers can't cache it across versions.
      filename: 'bundle.web.js',
      // TODO: chunking is not being used, everything is just stuffed into bundle.web.js.
      chunkFilename: '[name].chunk.js',
    },

    devtool,

    plugins: [
      new HtmlWebpackPlugin({
        title: 'MyProject',
        template: path.resolve(__dirname, 'index.html'),
        filename: path.resolve(__dirname, 'web/index.html'),
        inject: 'body',
        compile: true,
        favicon: false,
        minify: 'auto',
        cache: true,
        showErrors: true,
        chunks: 'all',
      }),
      new FaviconsWebpackPlugin(path.resolve(__dirname, 'assets/icon.png')),
      // TODO: this cannot be only dev, it needs to support production mode as well.
      // NOTE: I am not sure if this is being used anywhere.
      new DefinePlugin({
        'process.env': {
          NODE_ENV: '"development"',
          PUBLIC_URL: '""',
          APP_MANIFEST:
            '{"name":"MyProject","slug":"MyProject","platforms":["ios","android","web"],"version":"1.0.0","orientation":"portrait","icon":"./assets/icon.png","splash":{"image":"./assets/splash.png","resizeMode":"contain","backgroundColor":"#ffffff"},"updates":{"fallbackToCacheTimeout":0},"assetBundlePatterns":["**/*"],"web":{},"entryPoint":"index.web.js","sdkVersion":"37.0.0"}',
        },
        __DEV__: true,
      }),
      new WebpackManifestPlugin(),
      new CopyPlugin({
        patterns: [{from: 'fonts', to: 'fonts'}],
      }),
    ],

    module: {
      rules: [
        babelLoaderConfiguration,
        htmlLoaderConfiguration,
        fileLoaderConfiguration,
      ],
    },

    resolve: {
      alias: {
        'react-native$': 'react-native-web',
        'react-native/Libraries/Components/View/ViewStylePropTypes$':
          'react-native-web/dist/exports/View/ViewStylePropTypes',
        'react-native/Libraries/EventEmitter/RCTDeviceEventEmitter$':
          'react-native-web/dist/vendor/react-native/NativeEventEmitter/RCTDeviceEventEmitter',
        'react-native/Libraries/vendor/emitter/EventEmitter$':
          'react-native-web/dist/vendor/react-native/emitter/EventEmitter',
        'react-native/Libraries/vendor/emitter/EventSubscriptionVendor$':
          'react-native-web/dist/vendor/react-native/emitter/EventSubscriptionVendor',
        'react-native/Libraries/EventEmitter/NativeEventEmitter$':
          'react-native-web/dist/vendor/react-native/NativeEventEmitter',
        '@react-native-community/netinfo':
          'react-native-web/dist/exports/NetInfo',
      },
      extensions: [
        '.web.ts',
        '.web.tsx',
        '.web.mjs',
        '.web.js',
        '.web.jsx',
        '.ts',
        '.tsx',
        '.mjs',
        '.js',
        '.jsx',
        '.json',
        '.wasm',
      ],
    },
  };
};
@necolas
Copy link
Owner

necolas commented Jul 26, 2021

ERROR in ./node_modules/react-native/Libraries/Network/XMLHttpRequest.js 1:1656-1682

This tells you that you're trying to bundle react-native.

Read this documentation with the example of how to compile node_modules to replace react-native using webpack

https://necolas.github.io/react-native-web/docs/multi-platform/#compiling-and-bundling

@necolas necolas closed this as completed Jul 26, 2021
@lc3t35
Copy link

lc3t35 commented Aug 24, 2021

@necolas please reopen, can you please give a clear answer ?

alias works fine for

config.resolve.alias["./RCTAlertManager"] = "react-native-web/dist/exports/Alert";

but not for

config.resolve.alias["./RCTNetworking"] = "react-native-web/dist/exports/Network";

as there is no "Network" in exports.
Can you please give a better way to handle this ?

@lc3t35
Copy link

lc3t35 commented Aug 25, 2021

@fansilva1991 Hi Filipe, did you solve this issue on your side for RCTNetworking ?

@mpatafio
Copy link

mpatafio commented Feb 9, 2022

Same issue here with RCTNetworking, did you manage to solve it?

@barak109
Copy link

barak109 commented Apr 27, 2022

+1
@lc3t35 @mpatafio @fansilva1991 any solution?

@BenSherman228
Copy link

This may sound silly, but when I was looking for a solution to a similar question, I came across a simple article that gave the key to solving my problem. A great example of finding answers in the base.
"Why: A Guide to Finding and Using Causes" by Samantha Kleinberg - recommended.

@garvae
Copy link

garvae commented Dec 16, 2022

This might be helpful: storybookjs/storybook#6255 (comment)

flyskywhy added a commit to flyskywhy/GCanvasRNExamples that referenced this issue Dec 18, 2022
remove `react-native/Libraries/NewAppScreen` in `App.js` to
fix [Field 'browser' doesn't contain a valid alias configuration](necolas/react-native-web#2095);

use cross-env to let most package.json scripts can run on Windows;

since DISABLE_ESLINT_PLUGIN=true in `npm run web` , then we can use any version of eslint, so use eslint@7 even can be used in nodejs 10

upgrade eslint from 6.5.1 to 7.8.1 to
fix ESLint `Error: Failed to load plugin 'flowtype' declared in '.eslintrc.js » @react-native-community/eslint-config#overrides[0]': Cannot find module 'eslint/use-at-your-own-risk'`

not upgrade eslint to ^8.4.1 to
fix ESLint `TypeError: Module.createRequire is not a function` when using nodejs v10.15.3
@martin-yana
Copy link

no solutions?

@TClarke-AppDev
Copy link

any solutions to this yet? stuck on the same problem with the "Network" folder not being in exports.

@blwinters
Copy link

blwinters commented May 2, 2023

For people asking about issues with the missing RCTNetworking module, alias it to identity-obj-proxy as shown below (and install that package). If you actually need to call its functions, then you probably want to create a replacement module for it, similar to what I did with __webpack__/react-native-safe-area-context.js below.

I only use react-native-web for the purposes of Storybook, but I ran into these same errors when upgrading to Storybook v7, because that requires an upgrade to Webpack v5, which no longer includes Node polyfills. So in order to resolve these missing dependencies and fix the errors, I had to configure Webpack within the Storybook main.ts file.

It looks like @storybook/addon-react-native-web v7 is pretty straightforward if you use Expo, but my project does not.

I'm not an expert on this stuff, so maybe there is a better way to handle this, but here is my working file for reference (adjust as needed for your specific dependencies). The key updates to this file are:

  • modulesToAlias
    • identity-obj-proxy is a package that stands in when a RNW-compatible dependency does not exist
  • webpackFinal
    • resolve.modules: ../__webpack__ contains stub files for packages that were causing errors due to being bundled with webpack in the browser context. See below for an example with react-native-safe-area-context.js, but you can also just use export default {} in the file if the contents aren't invoked by your Storybook stories and you just need to prevent bundling errors.
    • resolve.fallback: polyfill node modules that are no longer handled by Webpack v5 (more details)
// .storybook/main.ts

import type { StorybookConfig } from '@storybook/react-webpack5'
import * as path from 'path'

const config: StorybookConfig = {
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [
    '@storybook/addon-links', 
    '@storybook/addon-essentials', 
    '@storybook/addon-interactions', 
    {
      name: '@storybook/addon-react-native-web',
      options: {
        modulesToTranspile: [
          '@dripsy',
          'dripsy',
          'strict-event-emitter',
          '@testing-library/jest-native',
          '@testing-library/react-native',
        ],
        modulesToAlias: { //modules without equivalent in react-native-web are aliased to identity-obj-proxy
          "../../Utilities/Platform": "react-native-web/dist/exports/Platform",
          "../Utilities/Platform": "react-native-web/dist/exports/Platform",
          "./Platform": "react-native-web/dist/exports/Platform",
          "../Utilities/BackHandler": "react-native-web/dist/exports/BackHandler",
          "../Components/AccessibilityInfo/legacySendAccessibilityEvent": "identity-obj-proxy",
          "../../Image/Image": "react-native-web/dist/exports/Image",
          "./RCTAlertManager": "react-native-web/dist/exports/Alert",
          "./RCTNetworking": "identity-obj-proxy",
          "./BaseViewConfig": "identity-obj-proxy",
          '../../StyleSheet/PlatformColorValueTypes': 'identity-obj-proxy',
          './PlatformColorValueTypes': 'identity-obj-proxy',
        }
      },
    }
  ],
  framework: {
    name: '@storybook/react-webpack5',
    options: {
      builder: {
        lazyCompilation: true,
      }
    },
  },
  staticDirs: [{ from: '../assets/fonts', to: 'fonts' }],
  docs: {
    autodocs: 'tag',
  },
  webpackFinal: config => { // custom config for storybook to work with react-native-web
    return {
      ...config,
      resolve: {
        ...config.resolve,
        modules: [
          path.resolve("./src"), ...config.resolve?.modules ?? [],
          "../__webpack__", // stub modules that shouldn't be bundled
        ],
        fallback: { // polyfill node modules
           timers: false,
           tty: false,
           os: false,
           http: false,
           https: false,
           zlib: false,
           util: false,
           stream: require.resolve("stream-browserify"),
           constants: require.resolve("constants-browserify"),
           ...config.resolve?.fallback,
        }
      }
    };
  }
}

export default config
// __webpack__/react-native-safe-area-context.js

import React from 'react'

export const useSafeAreaInsets = () => {
  return {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
  }
}

const SafeAreaContext = React.createContext({
  useSafeAreaInsets,
})

export default {
  SafeAreaProvider: SafeAreaContext.Provider,
  useSafeAreaInsets,
}

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

10 participants