React progects not working in windows 10? - javascript

I am working in MacBook Pro in React. The project works well. But I am running this project on Windows 10 and it drops a lot of errors. Switching property allowSyntheticDefaultImports in true
does not lead to success. An error: 'An export assignment cannot be used in a module with other exported elements.'
TSconfig:
{
"compilerOptions": {
"outDir": "build",
"module": "esnext",
"target": "es5",
"downlevelIteration": true,
"lib": ["es6", "dom", "es2019.object", "es2016.array.include"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "./src/",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"allowSyntheticDefaultImports" : false,
"plugins": [
{
"name": "typescript-tslint-plugin",
}
],
"baseUrl": "./src/",
"paths": {
"*": ["*"]
}
},
"exclude": [
"node_modules",
"cache",
"build",
"scripts",
"cypress",
"acceptance-tests",
"webpack",
"jest",
"config",
"src/setupTests.ts"
]
}

try adding "allowSyntheticDefaultImports": true in compilerOptions in
your tsconfig.json file and then use the syntax
Or try setting the esModuleInterop flag to true.
With flag esModuleInterop we can import CommonJS modules in compliance
with es6 modules spec
your file will look like this.
{
"compilerOptions": {
"outDir": "build",
"module": "esnext",
"target": "es5",
"downlevelIteration": true,
"lib": ["es6", "dom", "es2019.object", "es2016.array.include"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "./src/",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"allowSyntheticDefaultImports" : false,
"plugins": [
{
"name": "typescript-tslint-plugin",
}
],
"baseUrl": "./src/",
"paths": {
"*": ["*"]
}
},
"exclude": [
"node_modules",
"cache",
"build",
"scripts",
"cypress",
"acceptance-tests",
"webpack",
"jest",
"config",
"src/setupTests.ts"
],
"esModuleInterop": true, //<----------------------------add this
"allowSyntheticDefaultImports": true //<----------------------------and this
}

Related

Allow tsnd compile with errors in a JS file with #ts-check

I am migrating a backend project to use typescript with tsnd, I have configured the tsconfig.json like this:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"~/*": ["*"]
},
"declaration": true,
"esModuleInterop": true,
"module": "CommonJS",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"strict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "ES2019",
"outDir": "dist",
"types": ["node"],
"useUnknownInCatchVariables": false,
"allowJs": true,
"resolveJsonModule": true
},
"include": ["src"],
"exclude": []
}
That is the script I use to start the project:
"dev": "tsnd --clear -r tsconfig-paths/register src/index.ts"
But if I put // #ts-check in the top of a js file I get the error:
[ERROR] 16:42:50 тип Unable to compile TypeScript:
I would like to use // #ts-check in the js files because it helps me to identify problems, but at the same time I don't want the stop the compilation just because of that.
I found a solution:
in my tsconfig.json I can add inside the compilerOptions: "checkJs": true, then I don't need to add those // #ts-check in my js files anymore, then to allow the compilation of my project even with those errors in the js files, I can add also in my tsconfig.json:
"ts-node": {
"compilerOptions": {
"checkJs": false,
}
}
Keep in mind that is OUTSIDE of the compilerOptions.
At the end, this is how my tsconfig.json looks like:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"~/*": ["*"]
},
"declaration": true,
"esModuleInterop": true,
"module": "CommonJS",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"strict": true,
"alwaysStrict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "ES2019",
"outDir": "dist",
"types": ["node"],
"useUnknownInCatchVariables": false,
"allowJs": true,
"checkJs": true,
"resolveJsonModule": true,
"plugins": [
// Transform paths in output .js files
{ "transform": "typescript-transform-paths" },
// Transform paths in output .d.ts files (Include this line if you output declarations files)
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
],
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules"],
"ts-node": {
"compilerOptions": {
"checkJs": false,
}
}
}

error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS

I can't compile my TypeScript projects. These are my TSC
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es2020",
"module": "commonjs",
"jsx": "react",
"outDir": "Source/Bin",
"rootDir": "Source",
"noUnusedLocals": true,
"moduleResolution": "node",
"esModuleInterop": true,
"preserveSymlinks": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"stripInternal": true,
"removeComments": true,
"declaration": true,
"declarationDir": "Source/Bin/PDB",
},
"include": ["Source/"]
}
I googled the error online but can't find anything simiar.

Typescript compiler does not stop when a error occured in next js

i want use nextjs beside typescript in my project.
i used npx create-next-app --typescript(official way for add typescript to next).
everything ok but when a typescript error occured e.g const st:string =3; compiler successfully run and i recieved error only in my IDE and compiler don't stop.i want when run npm run dev command compiler show error to me and then failed.
I would be very grateful if anyone could help me
this is my tsconfig file
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
if you want better intellisense to figure out what's going on, make sure you enable strictNullChecks: true and alwaysStrict: true
That will enhance your TSX experience
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"moduleResolution": "node",
"noUnusedLocals": false,
"noUnusedParameters": false,
"jsx": "preserve",
"downlevelIteration": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": true,
"baseUrl": ".",
"allowJs": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"paths": {
"#/components/*": ["components/*"],
"#/config/*": ["config/*"],
"#/graphql/*": ["graphql/*"],
"#/lib/*": ["lib/*"],
"#/pages/*": ["pages/*"],
"#/scripts/*": ["scripts/*"],
"#/styles/*": ["styles/*"],
"#/test/*": ["test/*"],
"#/types/*": ["types/*"]
}
},
"include": ["**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
"exclude": ["node_modules", ".next", "out"]
}

Cannot read property 'text' of undefined ts-node esm

I'm trying to run my server locally during development. I just run my .ts files, I don't run the js files generated to my dist folder.
When I run this script though that lives in my src/client/package.json:
"start-static-server": "node --experimental-json-modules --loader ts-node/esm.mjs ../server/server.ts",
I'm getting this error. src/client/node_modules definitely has ts-node installed so not sure why this is complaining:
src/client/node_modules/ts-node/src/index.ts:594
return [output.outputFiles[1].text, output.outputFiles[0].text]
^
TypeError: Cannot read property 'text' of undefined
src/client/tsconfig.json
{
"compilerOptions": {
"noEmit": false,
"outDir": "../../dist",
"target": "ES2020",
"module": "esnext",
"lib": [
"es5",
"es6",
"dom"
],
"typeRoots": ["node_modules/#types"],
"moduleResolution": "node",
"allowJs": false,
"jsx": "react",
"noImplicitAny": false,
"sourceMap": false,
"inlineSources": false,
"removeComments": true,
"noUnusedLocals": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"preserveSymlinks": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"."
],
"exclude": [
"../node_modules",
"**/node_modules/*",
"node_modules/#types",
"node_modules/#types/*",
"node_modules/#types/**",
"js/ink-config.js",
"test/**/*",
"ext",
"../../dist",
"wallaby.ts",
]
}

Path aliases not seen by .spec.ts files in Visual Studio Code

I am having an issue where my path aliases (e.g.- import * as fromUsers from '#store/user/user.reducer';) are not being recognized within VS Code. (Note- on compiling everything works fine). VS Code reports "Cannot find module '#store/user/user.reducer'"
I have both a <root>/tsconfig.json, and a <root>/src/tsconfig.spec.json which look like:
tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["dom", "es2015"],
"baseUrl": "./src",
"paths": {
"#store/*": ["app/store/*"],
"#core/*": ["app/core/*"],
"#components/*": ["app/components/*"],
"#app/*": ["app/*"],
"#assets/*": ["assets/*"],
"#env": ["environments/environment"],
"#pages/*": ["pages/*"],
"#theme/*": ["theme/*"]
},
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"target": "es5"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "src/**/*.spec.ts", "src/**/__tests__/*.ts"],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
},
"types": ["jasmine"]
}
and src/tsconfig.spec.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"baseUrl": ".",
"module": "commonjs",
"target": "es5",
"allowJs": true,
"paths": {
"#store/*": ["app/store/*"],
"#core/*": ["app/core/*"],
"#components/*": ["app/components/*"],
"#app/*": ["app/*"],
"#assets/*": ["assets/*"],
"#env": ["environments/environment"],
"#pages/*": ["pages/*"],
"#theme/*": ["theme/*"]
}
},
"include": ["**/*.spec.ts"],
"exclude": ["node_modules"]
}
Any thoughts on how I can get VS Code to recognize those aliases, so that I dont keep seeing errors being reported in the IDE?

Categories