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.
Related
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
}
}
Yes, I know it's a very basic problem and that there are plenty of questions like this. But none sounds to be exactly the same as my case and I just can't wrap my mind around this issue. So here I go, I have the following files:
src/main.ts
import dotenv from "dotenv";
dotenv.config();
console.log("JWT_SALT", process.env.JWT_SALT);
package.json
{
"name": "family-budget-control",
"version": "1.0.0",
"description": "The back of of the Family Budget Control application",
"main": "index.js",
"type": "module",
"scripts": {
"start-watch": "gulp",
"compile": "npx tsc",
"start": "npx tsc && node compiled/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "davincif",
"license": "Zlib",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2"
},
"devDependencies": {
"#types/cors": "^2.8.12",
"#types/express": "^4.17.14",
"#types/node": "^18.11.9",
"del": "^7.0.0",
"gulp": "^4.0.2",
"gulp-nodemon": "^2.5.0",
"gulp-uglify": "^3.0.2",
"typescript": "^4.8.4"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "ES6",
"outDir": "compiled",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitOverride": true,
"skipLibCheck": true
},
"typeRoots": ["./node_modules/#types"],
"include": ["src/**/*"],
"exclude": ["node_modules", "compiled", "dist"]
}
But when running npm run compile, or npx tsc directly, I get this:
src/main.ts:1:20 - error TS2792: Cannot find module 'dotenv'. Did you
mean to set the 'moduleResolution' option to 'node', or to add aliases
to the 'paths' option?
1 import dotenv from "dotenv";
Yes, everyting is installed;
No, I can't use request();
No npm install --dotenv-extended didn't work.
I honestly feel I'm missing out something really obvious, but can't figure it out. Any help?
The user defined paths in tsconfig.json are not found – TypesScript says …
I have reinitialized the whole project. Used stock ts configs, checked every setting, updated everything. It just don't want to work.
Project structure:
|-./
|--index.ts
|--package.json
|--tsconfig.json
|--database/
|---dbOperations/
|----index.ts
|--api/
|---server.ts
package.json:
{
"name": "name",
"version": "0.1.0",
"private": true,
"description": "desc.",
"main": "index.ts ",
"scripts": {
"test": "jest --config jest.config.json",
"tsnode": "ts-node index.ts",
"lint": "eslint '**/*.ts' --ignore-pattern node_modules/",
},
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/preset-env": "^7.15.0",
"#babel/preset-typescript": "^7.15.0",
"#types/cors": "^2.8.12",
"#types/express": "^4.17.13",
"#types/jest": "^27.0.1",
"#typescript-eslint/eslint-plugin": "^4.32.0",
"#typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
"jest": "^27.1.0",
"nodemon": "^2.0.12",
"ts-jest": "^27.0.5",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
},
"dependencies": {
"express": "^4.17.1",
}
}
tsconfig.json (initial config after tsc --init with paths):
{
"compilerOptions": {
"strict": true,
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"#app/*": [
"./*"
],
"#api/*": [
"api/*"
],
"#database/*": [
"database/*"
],
"#dbOperations": [
"database/dbOperations/index.ts"
]
},
}
}
When I do import {x, y, z} from "#dbOperations" in /api/server.ts it won't work.
tsnode throws:
Error: Cannot find module '#dbOperations'
Any suggestions?
TypeScript only uses paths defined in tsconfig.json when compiling, but it doesn't actually output valid paths that are understandable for Node. You have to install tsconfig-paths to make it work.
I'm new to typescript my problem is that local module i created I can't use
./route/routes.ts
import routes from 'next-routes'
let route = new routes()
route.add('index', '/:name')
export default route
and in my server.ts
import route from "./route/routes"
but when i run npm run start then i get the following error
Cannot find module "./route/routes"
this is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"noImplicitAny": false
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}
and my package.json
{
"name": "Test",
"version": "1.0.0",
"description": "",
"main": "babel-node --presets es2015 server.ts",
"scripts": {
"dev": "node server.ts",
"build": "next build",
"postinstall": "npm run build",
"start": "NODE_ENV=production babel-node --presets es2015 server.ts"
},
"author": "Test",
"license": "",
"dependencies": {
"#zeit/next-sass": "^1.0.1",
"express": "^4.17.1",
"next": "^9.0.5",
"next-routes": "^1.4.2",
"node-sass": "^4.12.0",
"react": "^16.9.0",
"react-dom": "^16.9.0"
},
"devDependencies": {
"#types/express": "^4.17.11",
"#types/node": "^14.14.31",
"#types/react": "^17.0.2",
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"nextjs-sitemap-generator": "^0.1.1",
"nodemon": "^2.0.7",
"typescript": "^4.2.2"
}
}
i'm getting error since i migrate to typescript from js
Note: I try to find solution search online seems i can't find really good one
Thanks
Try moving routes.ts, to wherever your server.ts file is present, update the import statement, and see if it works
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": {