Is it possible to use export * from './X' with absolute import paths and eslint in react/typescript? I wanted to have single index.ts file in components folder that would export everything else but eslint gives me weird error when i'm trying to do this.
My current structure looks like this:
src/components/Example1/Example1.tsx
import React from 'react';
export type Props = {};
export const Example1 = (props: Props) => <div></div>
src/components/Example1/index.ts
export * from './Example1';
src/components/index.ts
export * from './Example1';
Both index files gives me error:
0:0 error Parsing error: Cannot read property 'name' of undefined
Also when i try to import anything from the components/index file using an absolute path import { Example1 } from 'components'; i have an error:
1:43 error Parse errors in imported module 'components': Cannot read property 'name' of undefined (undefined:undefined) import/namespace
1:43 error Parse errors in imported module 'components': Cannot read property 'name' of undefined (undefined:undefined) import/named
This is my .eslintrc.json file
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"root": true,
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:#typescript-eslint/eslint-recommended",
"plugin:import/warnings",
"plugin:import/errors",
"plugin:import/typescript",
"prettier/#typescript-eslint",
"prettier",
"prettier/react"
],
"plugins": [
"react",
"react-hooks",
"jsx-a11y",
"#typescript-eslint",
"prettier"
],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"settings": {
"react": {
"version": "detect"
},
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"],
"moduleDirectory": ["node_modules", "src/"]
}
}
},
"rules": {
"prettier/prettier": "error",
"import/newline-after-import": "error",
"no-unused-vars": "off",
"#typescript-eslint/no-unused-vars": [
"warn",
{ "args": "after-used", "argsIgnorePattern": "^_" }
]
}
}
This is my tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": "./src"
},
"include": ["src"]
}
My current package.json:
{
"name": "frontend",
"version": "0.0.0",
"license": "MIT",
"private": true,
"dependencies": {
"#apollo/react-hooks": "^3.1.3",
"#material-ui/core": "^4.9.5",
"#material-ui/icons": "^4.9.1",
"#material-ui/styles": "^4.9.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"#types/jest": "^24.0.0",
"#types/material-ui": "^0.21.7",
"#types/node": "^12.0.0",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"#types/react-router-dom": "^5.1.3",
"#types/styled-components": "^5.0.1",
"#types/yup": "^0.26.32",
"apollo-boost": "^0.4.7",
"env-cmd": "^10.1.0",
"formik": "^2.1.4",
"graphql": "^14.6.0",
"i18next": "^19.3.3",
"i18next-browser-languagedetector": "^4.0.2",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-i18next": "^11.3.3",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"styled-components": "^5.0.1",
"typescript": "~3.7.2",
"yup": "^0.28.3"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^2.23.0",
"#typescript-eslint/parser": "^2.23.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build:staging": "env-cmd -f .env.staging yarn build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "tsc --noEmit && eslint . --ext .js,.tsx,.ts --max-warnings=0",
"lint:fix": "eslint . --ext .js,.tsx,.ts --fix",
"watch": "react-scripts start",
"deploy": "yarn build && firebase deploy",
"deploy:staging": "yarn build:staging && firebase use staging && firebase deploy"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I'm also using lerna with yarn workspaces to easily handle MERN app.
Deleting eslint package / configs, node_modules did not help me, i still have Cannot read property 'name' of undefined error. It is super weird because in clean CRA everything works. I spent last 4 hours trying to solve this problem and finally used a workaround with importing types to root index file and reexporing them as new type but it's definately a painful solution. I need to keep isolatedModules. Maybe someone had similar problem?
The problem was #typescript-eslint package, check out the issue for more info:
https://github.com/typescript-eslint/typescript-eslint/issues/1746
Fast fix for this is to downgrade packages to version 2.22.0:
#typescript-eslint/eslint-plugin
#typescript-eslint/parser
#Edit This helped with lint error but my CRA build is still crashing, I guess i will have to wait for package fix
Related
I'm getting this error message:
./src/Home/home.scss (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-6-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--5-oneOf-6-3!./node_modules/sass-loader/dist/cjs.js??ref--5-oneOf-6-4!./src/Home/home.scss)
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (108)
For more information on which environments are supported please see:
https://github.com/sass/node-sass/releases/tag/v6.0.1
Here is my is my package.json:
{
"resolutions": { "react-error-overlay": "6.0.9" },
"dependencies": {
"#reduxjs/toolkit": "^1.6.1",
"#types/node": "^12.0.0",
"#types/react": "^17.0.0",
"#types/react-dom": "^17.0.0",
"#types/react-router-dom": "^5.1.8",
"node-sass": "^6.0.1",
"react": "^17.0.2",
"react-content-loader": "^6.0.3",
"react-dom": "^17.0.2",
"react-loading-skeleton": "^2.2.0",
"react-redux": "^7.2.4",
"react-router-dom": "^5.2.0",
"react-toastify": "^7.0.4",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"devDependencies": {
"#nomiclabs/hardhat-ethers": "^2.0.2",
"#nomiclabs/hardhat-etherscan": "^2.1.5",
"#nomiclabs/hardhat-waffle": "^2.0.1",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"chai": "^4.3.4",
"colors": "^1.4.0",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.4.5",
"hardhat": "^2.6.2",
"hardhat-deploy": "^0.9.3",
"react-scripts": "5.0.1"
},
"scripts": {
"start": "react-scripts --openssl-legacy-provider start",
"build": "./build.sh",
"build1": "set \"GENERATE_SOURCEMAP=false\" && react-scripts build",
"testui": "react-scripts test",
"eject": "react-scripts eject",
"compile": "npx hardhat compile",
"test": "npx hardhat test",
"clean": "rm -rf build/static/js/*.map",
"clean-win": "del build/static/js/*.map",
"local": "npx hardhat run --network localhost scripts/deploy-eth.js",
"ftm": "npx hardhat run --network ftm scripts/deploy-ftm.js",
"tftm": "npx hardhat run --network tftm scripts/deploy-ftm.js",
"btc": "npx hardhat run --network btc scripts/deploy.js",
"tbtc": "npx hardhat run --network localhost scripts/deploy.js"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"`your text`
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}`
}
This is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "es6", "es2017", "es2020", "dom"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"noEmit": true,
"jsx": "react-jsx",
"isolatedModules": true,
"noImplicitAny": false
},
"include": [
"src"
],
"typeRoots": [
"./node_modules/#types",
"./src/#types"
]
}
If you know about it, please send me answer.
Thank you.
Please help me with this:
My react application can run using npm start, everything is working fine with this command. However, when I try to deploy my application to google cloud, I am stuck at the step where I need to build the application using npm run build first.
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
Here is my package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#auth0/auth0-react": "^1.9.0",
"#emotion/react": "^11.9.0",
"#emotion/styled": "^11.8.1",
"#mui/icons-material": "^5.6.2",
"#mui/material": "^5.6.2",
"#mui/x-data-grid": "^5.12.0",
"#react-google-maps/api": "^2.12.1",
"#testing-library/jest-dom": "^5.16.3",
"#testing-library/react": "^12.1.4",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.4.1",
"#types/node": "^16.11.26",
"#types/react": "^18.0.15",
"#types/react-dom": "^17.0.14",
"#types/react-helmet": "^6.1.5",
"helmet": "^5.0.2",
"http-proxy-middleware": "^2.0.4",
"postcode-validator": "^3.6.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-google-recaptcha": "^2.1.0",
"react-helmet": "^6.1.0",
"react-native-map-clustering": "^3.4.2",
"react-native-maps": "^1.1.0",
"react-router-dom": "^6.2.2",
"react-scripts": "5.0.0",
"typescript": "^4.6.3",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build-css": "node combineCSS.js"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"type": "module",
"devDependencies": {
"#mui/x-data-grid-pro": "^5.15.2",
"#svgr/webpack": "^5.5.0",
"#types/google-map-react": "^2.1.7",
"#types/react-google-recaptcha": "^2.1.5",
"#types/react-modal": "^3.13.1",
"#types/react-router": "^5.1.18",
"#types/react-router-dom": "^5.3.3",
"#types/uuid": "^8.3.4",
"autoprefixer": "^10.4.4",
"axios": "^0.27.2",
"bootstrap": "^5.2.0",
"google-map-react": "^2.2.0",
"postcss": "^8.4.12",
"react-bootstrap": "^2.5.0",
"recharts": "^2.1.13",
"tailwindcss": "^3.0.23"
}
}
Here are some of my import component statement in the project
import { LoadingIcon } from './components/Icons'
import ScrollToTop from './components/ScrollToTop'
import InsightPage from './pages/InsightPage/InsightPage'
import Membership from './pages/MembershipPage/Membership'
Full detail in terminal after bulding fail
react-scripts build
Creating an optimized production build...
warn - The `content` option in your Tailwind CSS configuration is missing or empty.
warn - Configure your content sources or your generated CSS will be missing styles.
warn - https://tailwindcss.com/docs/content-configuration
Failed to compile.
The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script
Not much of detail but hopefully can help to figure the issue.
I think the issue is caused by the ts config file. However, I cannot find what config that makes this react application cannot be built using npm run build.
Any suggestions for this issue?
I'm converting my .js files to .tsx because I want to work with TypeScript.
I added TypeScript with yarn add typescript #types/node #types/react #types/react-dom #types/jest and started to convert index.tsx then App.tsx
With tsx extension I get this error :
Error: Cannot find module './App'
at webpackMissingModule (bundle.js:16:50)
at ./src/index.tsx (bundle.js:16:130)
at options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at startup:7:1
at startup:7:1
With .js extension, everything works fine.
Here is my package.json
{
"name": "workout_generator",
"version": "0.1.0",
"private": true,
"dependencies": {
"#emotion/react": "^11.10.0",
"#emotion/styled": "^11.10.0",
"#headlessui/react": "^1.6.6",
"#mui/material": "^5.9.3",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^14.4.2",
"#types/jest": "^28.1.6",
"#types/node": "^18.6.4",
"#types/react": "^18.0.15",
"#types/react-dom": "^18.0.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.4.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"typescript": "^4.7.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"autoprefixer": "^10.4.8",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.8"
}
}
Any idea where is my mistake ?
You need to inform webpack it needs to parse tsx files as well. I'm not familiar with Create-React-App, but if you run yarn eject you will have access to the webpack.config.js file.
In the field resolvejust add tsx:
resolve: {
extensions: [".ts", ".tsx", ".js", "jsx", ".json"]
}
You will also need a tsconfig.json file. It generally looks like this:
{
"compilerOptions": {
"outDir": "./build/",
"baseUrl": "./",
"skipLibCheck": true,
"noEmit": true,
"noImplicitAny": false,
"downlevelIteration": true,
"lib": ["es6", "es2019.array", "dom"],
"rootDir": "src",
"module": "commonjs",
"target": "es5",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"jsx": "react",
"allowJs": true
},
"include": ["./src/**/*", "./custom.d.ts"],
"exclude": ["node_modules"]
}
Last but not least, you will need to use #babel/preset-typescript in your .babelrc file.
{
"presets": [
"#babel/preset-env",
"#babel/preset-typescript",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties",
[
"#babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
If it sounds like too much trouble, I'd recommend using alright-react-app, or to recreate a project from scratch with the --typescript flag.
I am getting the error below for types.
I tried npm install #types/babel_types but that didn't fix it.
How do I go about fixing it? I am trying to compile a react project for electron.
Here is my package.json
{
"name": "abc",
"description": "abc",
"author": "abc",
"version": "1.4.11",
"main": "public/electron.ts",
"resolutions": {
"apollo-client": "2.6.3"
},
"dependencies": {
"#aws-amplify/cli": "^4.46.1",
"#types/react": "^16.9.43",
"#types/react-dom": "^16.9.8",
"#types/react-modal": "^3.12.0",
"#types/react-router-dom": "^5.1.7",
"#types/socket.io-client": "^1.4.36",
"#types/yup": "^0.29.11",
"aws-amplify": "^3.3.26",
"aws-amplify-react": "^4.2.30",
"aws-appsync": "^4.0.3",
"date-fns": "^2.22.1",
"electron-is-dev": "^2.0.0",
"electron-log": "^4.3.5",
"electron-updater": "^4.3.9",
"graphql-tag": "^2.12.4",
"node-sass": "^4.14.1",
"node-thermal-printer": "^4.1.2",
"npm": "^6.14.7",
"react": "^16.13.1",
"react-apollo-hooks": "^0.4.5",
"react-async": "^10.0.1",
"react-axios": "^2.0.5",
"react-dates": "^21.8.0",
"react-dom": "^16.13.1",
"react-load-script": "0.0.6",
"react-modal": "^3.12.1",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-script-hook": "^1.4.1",
"react-scripts": "^4.0.3",
"react-toastify": "^7.0.4",
"reshake": "^1.2.3",
"save": "^2.4.0",
"socket.io": "^4.1.2",
"socket.io-client": "^4.1.2",
"start": "^5.1.0",
"styled-components": "^5.3.0",
"typescript": "^3.9.7",
"yup": "^0.32.9"
},
"scripts": {
"start": "react-scripts start",
"start:web": "react-scripts start",
"build:web": "PUBLIC_URL=./ react-scripts build",
"start:desktop": "npm run build:desktop && electron ./build/electron.js",
"build:desktop": "tsc -p electron/tsconfig.json",
"test": "react-scripts test",
"eject": "react-scripts eject",
"release": "electron-builder -mwl -p 'onTagOrDraft'",
"release:windows": "electron-builder -w -p 'onTagOrDraft'",
"electron-dev": "concurrently \"BROWSER=none npm run start\" \"wait-on http://localhost:3000 && electron .\""
},
"eslintConfig": {
"extends": "react-app"
},
"devDependencies": {
"concurrently": "^5.3.0",
"electron": "^11.4.1",
"electron-builder": "^22.11.7",
"wait-on": "^5.2.1"
},
"build": {
"appId": "com.tabin.desktop",
"copyright": "Copyright 2021 Zeal Private Ltd",
"publish": {
"provider": "github"
},
"mac": {
"category": "public.app-category.business",
"icon": "build/icon.icns"
},
"win": {
"icon": "build/icon.ico",
"certificateFile": "private/electron-tabin-desktop-development.p12",
"certificatePassword": "",
"verifyUpdateCodeSignature": false
},
"linux": {
"category": "Office",
"icon": "build/icon.png"
}
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"noFallthroughCasesInSwitch": true
},
"include": [
"src",
"electron/**/*"
]
}
Please tell me how to get rid of this error. Do I need to create some .d.ts files?
Thanks,
First, try this solution.
Edit your TypeScript Config file (tsconfig.json) and add a new key-value pair as
"noImplicitAny": false
If this things doesn't work then here are two other solutions
When a module is not yours - try to install types from #types:
npm install -D #types/module-name
If the above install errors - try changing import statements to require:
// import * as yourModuleName from 'module-name';
const yourModuleName = require('module-name');
I'm running yarn test on a project and I get the following error:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default as a11yDark } from './a11y-dark';
^^^^^^
SyntaxError: Unexpected token 'export'
1 | import React, { PureComponent } from "react";
2 | import SyntaxHighlighter from "react-syntax-highlighter";
> 3 | import { idea } from "react-syntax-highlighter/dist/esm/styles/hljs";
my tsconfig.json is as follows:
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true
},
"include": [
"src"
]
}
I'm using ts-jest and my jest.config.js is set as:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
and my package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"engines": {
"node": "14.15.0"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.32",
"#fortawesome/free-brands-svg-icons": "^5.8.1",
"#fortawesome/free-solid-svg-icons": "^5.15.1",
"#fortawesome/react-fontawesome": "^0.1.4",
"#types/jest": "26.0.15",
"#types/node": "14.14.7",
"#types/node-fetch": "^2.5.7",
"#types/react": "17.0.0",
"#types/react-dom": "17.0.0",
"#types/react-router-dom": "5.1.6",
"#types/react-syntax-highlighter": "13.5.0",
"bootstrap": "^4.5.3",
"hydrogen": "0.2.0",
"node-fetch": "^2.6.1",
"react": "17.0.1",
"react-app-polyfill": "2.0.0",
"react-dom": "17.0.1",
"react-ga": "3.3.0",
"react-markdown": "5.0.3",
"react-router-dom": "5.2.0",
"react-scripts": "4.0.1",
"react-syntax-highlighter": "15.3.1",
"tslint": "^6.1.3",
"typescript": "4.1.2",
"url-search-params-polyfill": "8.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "tsc --noEmit -p . && react-scripts test --all",
"eject": "react-scripts eject",
"lint": "tslint --project ."
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"#types/enzyme": "^3.10.8",
"#types/enzyme-adapter-react-16": "^1.0.6",
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "^1.15.5"
}
}
not really sure on what am I doing wrong here, I've tried configuring jest and typescript but I got nowhere.
The answer was importing the module as commonjs instead of esm, as:
import idea from "react-syntax-highlighter/dist/cjs/styles/hljs/idea";