4

I'm not entirely sure why I am getting this error, as react is present in node_modules and imported in referenced file

enter image description here

Referred App.js file is this one

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const unstated_1 = require("unstated");
const layouts_1 = __importDefault(require("./layouts"));
const App = () => (<unstated_1.Provider>
    <layouts_1.default />
  </unstated_1.Provider>);
exports.default = App;

This is output from TypeScript ^. Non transpiled version is as follows

import React from 'react'
import { Provider as StateProvider } from 'unstated'
import AppRoot from './layouts'

const App = () => (
  <StateProvider>
    <AppRoot />
  </StateProvider>
)

export default App

My project structure looks like this

packages/
  native-app/
    node_modules/
    ios
    index.js
    src/
      App.tsx
    dist/
      native-app/
        src/
          App.js
      server/
  server/

I feel like this might be related to nesting inside dist folder? My main react native index.js imports App like this

import { AppRegistry } from 'react-native'
import App from './dist/native-app/src/App'

AppRegistry.registerComponent('MyApp', () => App)

Note: This looks like monorepo, but I am not using anything like yarn workspaces or lerna, packages are installed inside each folder i.e. native-app and server with some common devDependencies like typescript, tslint and prettier installed in root folder where packages are located.

Project uses following tsconfig

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["es2017"],

    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,

    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "alwaysStrict": true,

    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "exclude": [
    "scripts",
    "packages/native-app/dist",
    "packages/server/functions/dist"
  ]
}

And package json for native-app

{
  "name": "skimitar-app",
  "version": "1.0.0",
  "description": "Skimitar app",
  "private": true,
  "dependencies": {
    "asq-react-native-device": "1.2.2",
    "asq-react-native-facebook-log-in": "1.1.0",
    "asq-react-native-google-sign-in": "1.2.0",
    "asq-react-native-sensors": "1.1.0",
    "react": "16.4.2",
    "react-native": "0.56.0",
    "react-native-firebase": "4.3.8",
    "react-native-svg": "6.5.2",
    "unstated": "2.1.1"
  },
  "devDependencies": {
    "@types/react": "16.4.13",
    "@types/react-native": "0.56.17"
  }
}
3
  • Can I see your package.json? Commented Sep 10, 2018 at 8:35
  • Still the same? Commented Sep 10, 2018 at 8:42
  • @xSkrappy should be at the bottom of the question, maybe refresh needed? Commented Sep 10, 2018 at 8:43

2 Answers 2

16

When using React with TypeScript, change your React import statements like so:

import * as React from 'react';

You could also change the module option in the tsconfig.json file to "es2015":

{
"compilerOptions": {
  "module": "es2015",
  ...
Sign up to request clarification or add additional context in comments.

2 Comments

This works but do you have to do this everywhere? In some files I've got import React, { useEffect } from 'react'
You would either need to change to using the same import or as mentioned elsewhere you might be able to set allowSyntheticDefaultImports in tsconfig but this might only work depending on how your project is being built.
0

it’s possible to avoid using ‘import * as …’ by setting “allowSyntheticDefaultImports”: true in the ‘compilerOptions’ section in tsconfig.json

2 Comments

setting “allowSyntheticDefaultImports”: true did not work.
As docs says allowSyntheticDefaultImports (typescriptlang.org/docs/handbook/compiler-options.html) don't take any effect of the emit code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.