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

React native package main field related error. #404

Closed
cmnstmntmn opened this issue Jun 26, 2020 · 13 comments
Closed

React native package main field related error. #404

cmnstmntmn opened this issue Jun 26, 2020 · 13 comments
Labels
⚑ needs info Issues that need more explanation to understand question Issues that are usage questions

Comments

@cmnstmntmn
Copy link

Hei,

I'm trying to plug supertruct to a react-native app, but i'm getting this error

error: Error: While trying to resolve module `superstruct` from file `validation.js`, the package `node_modules/superstruct/package.json` was successfully found. 
However, this package itself specifies a `main` module field that could not be resolved (`node_modules/superstruct/index.cjs`. Indeed, none of these files exist:
@cmnstmntmn
Copy link
Author

any workarounds for this ?

@cmnstmntmn
Copy link
Author

i found this

@ianstormtaylor ianstormtaylor added question Issues that are usage questions ⚑ needs info Issues that need more explanation to understand labels Nov 24, 2020
@roccomuso
Copy link

any news on this?

@rhythnic
Copy link
Contributor

rhythnic commented Dec 11, 2020

package.json is using type:module. For recent versions of Node.js, this means that the entrypoint to the module should be an ECMAScript module; however, main is set to a CommonJS file. This makes Superstruct unusable in any Node.js package of type:module. It's possible to support both ECMAScript modules and CommonJS by using conditional exports.

// package.json
{
  // keep pointing main to cjs for legacy versions of node.js
  "main": "./lib/index.cjs",
  // use conditional exports for recent versions of node.js
  "exports": {
    "import": "./lib/index.es.js",
    "require": "./lib/index.cjs"
  },
  "type": "module"
}

@isnifer
Copy link

isnifer commented Jan 1, 2021

@rhythnic why did you mention Node.js? The question was about React-Native. Your PR does not help to fix superstruct for RN. @ianstormtaylor too early to close this issue.

@isnifer
Copy link

isnifer commented Jan 1, 2021

But this config for metro bundler helped me:

// https://github.com/ioveracker/demo-react-native-emotion/blob/master/metro.config.js

const { getDefaultConfig } = require('metro-config');

module.exports = (async () => {
    const {
        resolver: { sourceExts },
    } = await getDefaultConfig();
    return {
        resolver: {
            // Add cjs extension so stylis will load.
            sourceExts: [...sourceExts, 'cjs'],
        },
        transformer: {
            getTransformOptions: async () => ({
                transform: {
                    experimentalImportSupport: false,
                    inlineRequires: false,
                },
            }),
        },
    };
})();

@gabrielgrover
Copy link

gabrielgrover commented Jan 28, 2021

I am using expo for my react native project and this worked for me

const { getDefaultConfig } = require('@expo/metro-config');

const defaultConfig = getDefaultConfig(__dirname);

module.exports = {
  resolver: {
    sourceExts: [...defaultConfig.resolver.sourceExts, 'cjs'],
  },
};

@leolangen
Copy link

We have a different version of metro-config in our project, using RN version 0.61.2. The following worked for me

...
const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts 

module.exports = {
  ...
  resolver: {
    sourceExts: [...defaultSourceExts, 'cjs'],
    ...
  }
}

@Thanaen
Copy link

Thanaen commented May 31, 2021

Hello !
Question: why was the issue closed? The problem still exists today, just create a new react project and add the package to reproduce it!

@isnifer
Copy link

isnifer commented May 31, 2021

@Thanaen man, just read a comment above yours

@Thanaen
Copy link

Thanaen commented Jun 1, 2021

@Thanaen man, just read a comment above yours

Hello @isnifer ,
If we need to modify a react-native configuration file to make the package work, can we consider the issue resolved? 😕

@isnifer
Copy link

isnifer commented Jun 1, 2021

@Thanaen yes. Author just decided to use "standard" extension for non es-modules files.
Anyway, I don't care cause I decided don't use this package

@matomesc
Copy link

I'm getting the following error after using the workaround above using react-native 0.69.5 and expo
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚑ needs info Issues that need more explanation to understand question Issues that are usage questions
Projects
None yet
Development

No branches or pull requests

9 participants