I am trying to setup yarn workspace in an existing project. When running yarn workspace #project/telemetry start:dev TS errors are triggered in a completely different project #project/web.
error TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.
My project structure is the following:
project
/node_modules
/packages
/common
/web
/telemetry
tsconfig.json
yarn.lock
I was able to run yarn workspace #project/web dev (Next.js) application after some tweaking with tsconfig and resolution. I was able to share a common lib to it as well.
The next step is to share the same common lib with telemetry (nest.js) service, but when running the service TS errors are triggered for /web somehow even though /telemetry never uses anything from /web.
project/package.json
{
"name": "#project/root",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"workspaces": [
"packages/*"
],
"resolutions": {
"#types/react-dom": "^17.0.11",
"#types/react": "17.0.38"
}
}
project/tsconfig.json
{
"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": "react"
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"types.d.ts"
]
}
project/packages/web/package.json
{
"name": "#project/web",
"private": true,
"version": "1.0.0",
"scripts": {
"dev": "env-cmd -f environments/.env.development next dev",
},
"dependencies": {
"#project/common": "*", // Shared common
"micro": "^9.3.4",
"next": "12.2.0",
"next-connect": "^0.11.0",
"react": "17.0.2",
"react-dom": "17.0.2",
},
"devDependencies": {
"#babel/core": "^7.16.7",
"#testing-library/jest-dom": "^5.16.1",
"#testing-library/react": "^12.1.2",
"#types/enzyme": "^3.10.11",
"#types/jest": "^27.4.0",
"#types/node": "17.0.8",
"#types/react": "17.0.38",
"#types/react-dom": "^17.0.11",
"#typescript-eslint/eslint-plugin": "^5.20.0",
"#typescript-eslint/parser": "^5.20.0",
"#wojtekmaj/enzyme-adapter-react-17": "^0.6.6",
"babel-loader": "^8.2.3",
"env-cmd": "^10.1.0",
"enzyme": "^3.11.0",
"eslint": "^8.6.0",
"eslint-config-next": "12.0.7",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"fork-ts-checker-webpack-plugin": "^6.5.0",
"husky": "^7.0.0",
"jest": "^27.4.7",
"lint-staged": "^12.1.7",
"next-transpile-modules": "^9.1.0",
"tsc-files": "^1.1.3",
"typescript": "4.5.4"
},
"lint-staged": {
"**/*.{ts,tsx}": [
"tsc-files --noEmit --pretty"
],
"*.{js,ts,jsx,tsx}": [
"eslint --cache --fix"
]
}
}
project/packages/web/tsconfig.json
{
"extends": "../../tsconfig.json",
"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",
"incremental": true,
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"**/*.css"
],
"exclude": ["node_modules"],
"typeRoots": [
"./global.d.ts",
"./node_modules/#types"
]
}
project/packages/telemetry/package.json
{
"name": "#project/telemetry",
"version": "1.0.0",
"description": "",
"author": "",
"private": true,
"scripts": {
"start:dev": "nest start --watch"
},
"dependencies": {
"#project/common": "*", // Shared common
"#nestjs/common": "^9.0.0",
"#nestjs/config": "^2.2.0",
"#nestjs/core": "^9.0.0",
"#nestjs/mapped-types": "*",
"#nestjs/mongoose": "^9.2.0",
"#nestjs/platform-express": "^9.0.0",
"cache-manager": "^4.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"express": "^4.18.1",
"helmet": "^6.0.0",
"mongoose": "^6.6.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
},
"devDependencies": {
"#nestjs/cli": "^9.0.0",
"#nestjs/schematics": "^9.0.0",
"#nestjs/testing": "^9.0.0",
"#types/express": "^4.17.13",
"#types/jest": "28.1.8",
"#types/node": "^16.0.0",
"#types/supertest": "^2.0.11",
"#typescript-eslint/eslint-plugin": "^5.0.0",
"#typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.3",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.8",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.0",
"typescript": "~4.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
project/packages/telemetry/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
Related
i have short and simple example of kafkaJs consumer, its works well on manual node build. But when im trying to start this on NX build i have that error:
"TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/tarasliush/Documents/Hailer/hailer-integration/packages/digione/src/app/consumers/studentConsumer.ts"
TS-config file in root:
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {}
},
"exclude": ["node_modules", "tmp"]
}
Package.json:
"
{
"name": "hailer-integration",
"version": "0.0.0",
"license": "MIT",
"type": "module",
"scripts": {},
"private": true,
"devDependencies": {
"#nrwl/esbuild": "15.6.3",
"#nrwl/eslint-plugin-nx": "15.6.3",
"#nrwl/jest": "15.6.3",
"#nrwl/js": "15.6.3",
"#nrwl/linter": "15.6.3",
"#nrwl/node": "^15.6.3",
"#nrwl/workspace": "15.6.3",
"#types/express": "4.17.13",
"#types/jest": "28.1.1",
"#types/node": "18.7.1",
"#typescript-eslint/eslint-plugin": "^5.36.1",
"#typescript-eslint/parser": "^5.36.1",
"esbuild": "^0.15.7",
"eslint": "~8.15.0",
"eslint-config-prettier": "8.1.0",
"express": "^4.18.1",
"jest": "28.1.1",
"jest-environment-jsdom": "28.1.1",
"nx": "15.6.3",
"prettier": "^2.6.2",
"ts-jest": "28.0.5",
"ts-node": "10.9.1",
"typescript": "~4.8.2"
},
"dependencies": {
"axios": "^1.0.0",
"kafkajs": "^2.2.3",
"tslib": "^2.3.0"
}
}
"
I understood its some kind problem with TS-config file but cant resolve that.
THNX!
I'm trying to use jest on a vite+vue3+typescript project.
Unfortunatly jest continue to fail with a 'Cannot find module 'ts-jest/dist/config/config-set' message.
The file 'ts-jest/dist/config/config-set' don't exists in my nodeModules but I don't know how to install it. It would be the simpliest way to resolve this bug I hope.
Here's my package.json configuration:
"dependencies": {
"#types/jest": "^29.1.0",
"#vue/composition-api": "^1.7.1",
"#vue/test-utils": "^2.0.0-rc.18",
"babel-jest": "27.5.1",
"fuzzysort": "^2.0.1",
"jest": "^29.1.1",
"ts-jest": "^29.0.3",
"vue": "^3.2.37"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^5.38.1",
"#typescript-eslint/parser": "^5.38.1",
"#vitejs/plugin-vue": "^3.1.0",
"#vue/eslint-config-typescript": "^11.0.2",
"#vue/vue3-jest": "27.0.0",
"eslint": "^8.22.0",
"eslint-plugin-vue": "^9.5.1",
"sass": "^1.55.0",
"sass-loader": "^13.0.2",
"typescript": "^4.8.4",
"vite": "^3.1.0",
"vue-tsc": "^0.40.4"
},
"moduleDirectories": [
"node_modules",
"src"
]
jest.config.js:
module.exports = {
moduleFileExtensions: [
'js',
'ts',
'json',
'vue'
],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.vue$': '#vue/vue3-jest'
},
testMatch: [
'**/(src)/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)',
],
}
And the tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"types": ["vite/client", "#types/jest"],
"skipLibCheck": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json"}]
}
It works with:
package.json
{
"name": "MerxBiblio",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"serve": "vite preview",
"test": "jest src"
},
"dependencies": {
"fuzzysort": "^2.0.1",
"vue": "^3.2.16"
},
"devDependencies": {
"#types/jest": "^27.0.2",
"#vitejs/plugin-vue": "^1.9.3",
"#vue/test-utils": "^2.0.0-rc.15",
"#vue/vue3-jest": "^27.0.0-alpha.1",
"jest": "^27.2.5",
"ts-jest": "^27.0.5",
"typescript": "^4.4.3",
"vite": "^2.6.4",
"vue-tsc": "^0.3.0",
"sass": "^1.55.0",
"sass-loader": "^13.0.2"
}
}
jest.config.js
moduleFileExtensions: [
'js',
'ts',
'json',
'vue'
],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.vue$': '#vue/vue3-jest'
},
testEnvironment: "jsdom"
tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"useDefineForClassFields": true,
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"types": ["#types/jest"]
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}
I am facing issues when running a *.ts file using ts-node.
The directory structure of my project looks like this.
-- project dir
-- src
-- services
-- module1
-- file1.ts (classA)
-- file2.ts (classB)
-- index.ts (contains export statements like export * from file1.ts)
-- application.ts
And here's what I have configured in my tsconfig.json file.
{
"compilerOptions": {
"lib": [
"es2020",
"dom"
],
"baseUrl": ".",
"paths": {
"#src/*": ["src/*"],
"#tests/*": ["tests/*"],
},
"module": "CommonJS",
"target": "es2020",
"strict": true,
"alwaysStrict": true,
"declaration": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"noImplicitReturns": true,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/#types"
]
},
"include": [
"src/**/*.ts",
"tests/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Here's what I have in my package.json.
{
"name": "xxxx",
"version": "1.0.0",
"description": "xxxx",
"main": "index.js",
"scripts": {
"export": "ts-node src/application.ts",
"build": "tsc",
"watch": "tsc -w",
"lint": "eslint . --ext ts --cache --fix",
"test": "jest --ci --verbose=false --config jest.config.json --passWithNoTests",
"migrate": "ts-node src/application.ts"
},
"author": "xxxx",
"devDependencies": {
"#types/jest": "^27.5.0",
"#types/minimist": "^1.2.2",
"#types/node": "^17.0.31",
"#typescript-eslint/eslint-plugin": "^5.22.0",
"#typescript-eslint/parser": "^5.22.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.1.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"jest": "^28.0.3",
"minimist": "^1.2.6",
"prettier": "^2.6.2",
"ts-jest": "^28.0.1",
"ts-node": "^10.7.0",
"typescript": "^4.6.4"
},
"dependencies": {
"axios": "^0.27.2",
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
"retry-axios": "^3.0.0",
"sequelize-typescript": "^2.1.3"
}
}
In my application.ts I am importing classes like this.
import {classA} from "#src/services/module1";
But it keeps failing with following error.
Error: Cannot find module '#src/services/module1'
I've been searching for days and have tried heaps of solutions provided in other answers but nothing worked for me. I would appreciate if anyone can guide me in the right direction.
After cloning the repo from GitHub and installing packages by running yarn install, when I'm run the project getting this error. I tried to update react script latest, doesn't work. Also, tried to update typescript 4.1.2 doesn't work.
Node v: 14.17.3
Npm v: 6.14.13
Yarn v: 1.22.10
This is my package.json
{
"name": "zis3-frontend",
"version": "4.6.41.3",
"created": "2022-03-15 20:23:03",
"private": true,
"dependencies": {
"#ant-design/charts": "^1.0.8",
"#ant-design/icons": "^4.1.0",
"#types/i18next": "^13.0.0",
"#types/react-highlight-words": "^0.16.1",
"#types/react-redux": "^7.1.8",
"#types/react-router": "^5.1.7",
"#types/react-router-dom": "^5.1.5",
"antd": "^4.10.2",
"antd-button-color": "^1.0.3",
"antd-mask-input": "^0.1.13",
"array-move": "^4.0.0",
"axios": "^0.19.2",
"fast-xml-parser": "^3.20.3",
"i18next": "17.0.1",
"i18next-browser-languagedetector": "^4.1.1",
"i18next-http-backend": "^1.0.8",
"moment": "^2.25.3",
"node-sass": "4.14.1",
"query-string": "^6.13.0",
"react": "^16.13.1",
"react-csv": "^2.2.2",
"react-dom": "^16.13.1",
"react-highlight-words": "^0.16.0",
"react-i18next": "10.11.0",
"react-icons": "^4.1.0",
"react-json-pretty": "^2.2.0",
"react-quill": "^1.3.5",
"react-redux": "^7.2.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.3",
"react-sortable-hoc": "^2.0.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"sweetalert2": "^11.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts --max_old_space_size=4096 build && REACT_APP_BUILD_VERSION=$(./ci/get_fe_version.py) && sed -i -- 's/%REACT_APP_BUILD_VERSION%/'$REACT_APP_BUILD_VERSION'/g' build/index.html && echo React Build Version = $REACT_APP_BUILD_VERSION",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"postcssSortingConfig": {
"properties-order": "alphabetical"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"prettier": "2.0.5",
"stylelint": "^13.3.3",
"stylelint-config-sass-guidelines": "^7.0.0",
"stylelint-config-standard": "^20.0.0",
"stylelint-statement-max-nesting-depth": "^2.0.1",
"typescript": "^3.9.7"
}
}
TS Config
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"jsx": "react",
"lib": ["dom", "dom.iterable", "scripthost", "es5", "es6"],
"baseUrl": "./src",
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"outDir": "./dist",
"rootDir": "./src",
"removeComments": true,
"noEmitOnError": true,
"noImplicitAny": false,
"noLib": false,
"preserveConstEnums": true,
"declaration": false,
"suppressImplicitAnyIndexErrors": true,
"sourceMap": true
},
"filesGlob": ["./src/**/*.ts", "./src/**/*.tsx", "!./node_modules/**"],
"include": ["./src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"],
"rules": {
"quotemark": "single",
"autoFixOnSave": true
}
}
Eg. I did the following
npm uninstall -D typescript (This uninstalled 3.9.7 version)
npm i -D typescript#4.2.4
This fixed the problem
I would like to import url and querystring node modules in a TypeScript Application.
import url from 'url';
import querystring from 'querystring';
But I'm getting the fallowing warning, and I'm not getting typing from it.
[ts] Could not find a declaration file for module 'url'. '.../node_modules/url/url.js' implicitly has an 'any' type.
Try npm install #types/url if it exists or add a new declaration (.d.ts) file containing declare module 'url'; [7016]
I know that url and querystring are node modules. I've Installed "#types/node": "^10.12.9", but it does not solves the typing from both neither the warning.
What Should I Do to get typing from it?
info: I'ts a frontend application using Vue CLI
here are my ts.config
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"strict": true,
"strictNullChecks": false,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noImplicitAny": false,
"baseUrl": ".",
"types": ["webpack-env", "mocha", "chai"],
"paths": {
"#/*": ["src/*"]
},
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"],
"exclude": ["node_modules"]
}
and package.json
{
"name": "rating-form",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test:unit": "vue-cli-service test:unit"
},
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.1.3",
"jquery": "^3.3.1",
"moment": "^2.22.2",
"popper.js": "^1.14.4",
"toastr": "^2.1.4",
"vue": "^2.5.17",
"vue-class-component": "^6.0.0",
"vue-property-decorator": "^7.0.0",
"vue-router": "^3.0.1",
"vue-slider-component": "^2.8.0"
},
"devDependencies": {
"#types/chai": "^4.1.0",
"#types/mocha": "^5.2.4",
"#types/node": "^10.12.9",
"#types/qs": "^6.5.1",
"#types/toastr": "^2.1.35",
"#vue/cli-plugin-typescript": "^3.1.1",
"#vue/cli-plugin-unit-mocha": "^3.1.1",
"#vue/cli-service": "^3.1.1",
"#vue/test-utils": "^1.0.0-beta.20",
"chai": "^4.1.2",
"ts-node": "^7.0.1",
"typescript": "^3.1.6",
"vue-template-compiler": "^2.5.17"
}
}
In tsconfig.json, you have to either add "node" to "types" array:
"baseUrl": ".",
"types": ["webpack-env", "mocha", "chai", "node"],
"paths": {
… or remove the array altogether (which is what I would do):
"baseUrl": ".",
"paths": {