2

I'm trying to add typescript to a existing react project but I'm getting an error when re-starting the server with npm start.

I'm following the steps from this documentation but I'm getting an error message:

enter image description here

The only thing I might not have right is the react.scripts versions. Here is a copy of the results thrown by npm outdated:

enter image description here

Also, I checked my dependencies and everything seems to be normal.

Things I have done:

  • run npm update to update all react related packages
  • I have deleted the node files and re install them with npm install
  • I have renamed other files to .tsx including the index.js file but I get another error:

enter image description here

Really keen to move this project to the next level with typescript but I have been stack trying to get it to work. By the way, here are my dependencies too:

{
  "name": "world-budget-react-v",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.34",
    "@fortawesome/free-brands-svg-icons": "^5.15.2",
    "@fortawesome/free-solid-svg-icons": "^5.15.2",
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@types/jest": "^26.0.23",
    "@types/node": "^15.0.2",
    "@types/react": "^17.0.5",
    "@types/react-dom": "^17.0.3",
    "@types/react-redux": "^7.1.16",
    "autoprefixer": "7.1.6",
    "axios": "^0.21.1",
    "babel-core": "6.26.0",
    "babel-eslint": "7.2.3",
    "babel-jest": "20.0.3",
    "babel-loader": "7.1.2",
    "babel-preset-react-app": "^3.1.2",
    "babel-runtime": "6.26.0",
    "case-sensitive-paths-webpack-plugin": "2.1.1",
    "chalk": "1.1.3",
    "css-loader": "0.28.7",
    "dotenv": "4.0.0",
    "dotenv-expand": "4.2.0",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.6",
    "eslint": "4.10.0",
    "eslint-config-react-app": "^2.1.0",
    "eslint-loader": "1.9.0",
    "eslint-plugin-flowtype": "2.39.1",
    "eslint-plugin-import": "2.8.0",
    "eslint-plugin-jsx-a11y": "5.1.1",
    "eslint-plugin-react": "7.4.0",
    "extract-text-webpack-plugin": "3.0.2",
    "file-loader": "1.1.5",
    "firebase": "^8.2.6",
    "firebase-admin": "^9.4.2",
    "firebase-functions": "^3.13.1",
    "fs-extra": "3.0.1",
    "html-webpack-plugin": "2.29.0",
    "jest": "20.0.4",
    "object-assign": "4.1.1",
    "postcss-flexbugs-fixes": "3.2.0",
    "postcss-loader": "2.0.8",
    "promise": "8.0.1",
    "raf": "3.4.0",
    "react": "^17.0.2",
    "react-dev-utils": "^5.0.2",
    "react-dom": "^17.0.2",
    "react-gtm-module": "^2.0.11",
    "react-redux": "^7.2.2",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "react-scroll-restoration": "^1.0.6",
    "react-test-renderer": "^17.0.1",
    "redux": "^4.1.0",
    "redux-thunk": "^2.3.0",
    "resolve": "1.6.0",
    "style-loader": "0.19.0",
    "sw-precache-webpack-plugin": "0.11.4",
    "typescript": "^4.2.4",
    "url-loader": "0.6.2",
    "webpack": "3.8.1",
    "webpack-dev-server": "2.11.3",
    "webpack-manifest-plugin": "1.3.2",
    "whatwg-fetch": "2.0.3"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js --env=jsdom"
  },
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
    ],
    "testEnvironment": "node",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  },
  "eslintConfig": {
    "extends": "react-app"
  }
}

Any recommendations? I hope I provided as much info as possible!

Lastly, I found this answer but not sure what they mean. Anyone keen to explain maybe Could not find a required file. - Adding TypeScript to React project

3
  • I think error is related to import statement, maybe your project is trying to import component from ./src/containers/ItemsHolder/ItemsHolder.js and it does not find it because you have renamed it to .tsx Commented May 8, 2021 at 11:43
  • Yes, this is because I tired to do it this way before, but it does not find the file. I think the error happens when the compiling process happen but I can't find anything related to this on line. I was thinking it was an old version of react. Commented May 8, 2021 at 11:45
  • The link you followed is specifically for create-react-app, so it's not going to work "out of the box" for you. But, I think you are close by renaming everything to .tsx. You will likely have to modify your webpack config to use index.tsx instead of index.js as its entry point. Leave your build.js and start.js as JS and check their contents for references that need to be updated to .tsx Commented May 9, 2021 at 0:28

2 Answers 2

0

As per the react-scripts 2.10 you can add TypeScript to an existing project.

yarn add typescript @types/node @types/react @types/react-dom @types/jest --dev
Sign up to request clarification or add additional context in comments.

1 Comment

I have added this with npm, the error appears when I then run npm start to restar the server.
0

Create a react-native app using : npx react-native init AwesomeProject

Once its up and running, follow this link to add typescript the right way.

2 Comments

But this is to create a new project with react. I want to migrate the current project to it!
@CarlosEscobar Skip the initialising and pre-req part... I have updated the link in my solution. Check that out. Complete all the steps till the bottom the the page.

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.