ERROR: Error reading "src/core/database/config.ts" - node.js

I want to migrate db in Node Nest.JS application.
npx sequelize-cli db:migrate shell command
throws an the below exception:
Exception detail:
Error: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/../src/core/database/config.ts
config.ts File
module.exports = {
development: {
default: {
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_GLOBAL,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
},
transaction: {
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_TRANSACTION,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
},
},
test: {
default: {
...
},
transaction: {
...
},
},
staging: {
default: {
...
},
transaction: {
...
},
},
production: {
default: {
...
},
transaction: {
...
},
},
};
.sequelizerc file
const path = require('path');
require('dotenv').config();
module.exports = {
'seeders-path': path.resolve('src/core/database', 'seeders'),
'migrations-path': path.resolve('src/core/database', 'migrations'),
config: path.resolve('src/core/database', 'config.ts'),
};
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,
"resolveJsonModule": true,
}
}
package.json
{
"name": "name",
"version": "1.0.0",
"description": "des",
"author": "auth",
"repository": {
"type": "git",
"url": "https://....git"
},
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "NODE_ENV=development nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "cp -rv dist/i18n dist/src/ && nest start",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "NODE_ENV=test jest --config ./test/jest-e2e.json",
"db:seed": "npx sequelize-cli db:seed:all",
"db:migrate": "npx sequelize-cli db:migrate",
"testdb:seed": "NODE_ENV=test npx sequelize-cli db:seed:all",
"testdb:migrate": "NODE_ENV=test npx sequelize-cli db:migrate",
"copy-assets": "cp -rv dist/i18n dist/src/"
},
"dependencies": {
"#elastic/ecs-winston-format": "^1.3.1",
"#nestjs-modules/mailer": "^1.6.0",
"#nestjs/axios": "^0.0.5",
"#nestjs/common": "^8.0.0",
"#nestjs/config": "^1.1.0",
"#nestjs/core": "^8.0.0",
"#nestjs/jwt": "^8.0.0",
"#nestjs/passport": "^8.0.1",
"#nestjs/platform-express": "^8.0.0",
"#nestjs/sequelize": "^8.0.0",
"#nestjs/serve-static": "^2.2.2",
"#nestjs/swagger": "^5.1.4",
"#nestjs/throttler": "^2.0.0",
"#sentry/node": "^5.9.0",
"amazon-cognito-identity-js": "^5.2.3",
"aws-sdk": "^2.1048.0",
"bcrypt": "^5.0.1",
"class-transformer": "0.4.0",
"class-validator": "^0.13.1",
"cookie-parser": "^1.4.6",
"dotenv": "^10.0.0",
"elasticsearch": "^16.7.2",
"faker": "^5.5.3",
"firebase": "^9.5.0",
"firebase-admin": "^10.0.0",
"generate-password": "^1.7.0",
"handlebars": "^4.7.7",
"helmet": "^4.6.0",
"json2csv": "^5.0.6",
"jsonwebtoken": "^8.5.1",
"jwk-to-pem": "^2.0.5",
"lodash": "^4.17.21",
"moment": "^2.29.1",
"multer": "^1.4.4",
"nest-winston": "^1.6.2",
"nestjs-i18n": "^8.2.1",
"nestjs-redis": "^1.3.3",
"nestjs-throttler-storage-redis": "^0.1.18",
"nodemailer": "^6.7.2",
"passport": "^0.4.0",
"passport-firebase-jwt": "^1.2.1",
"passport-jwt": "^4.0.0",
"pg": "^8.7.1",
"pg-hstore": "^2.3.4",
"redis": "^4.0.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"sequelize": "^6.11.0",
"sequelize-cli": "^6.3.0",
"sequelize-typescript": "^2.1.1",
"swagger-ui-express": "^4.1.6",
"uuid": "^8.3.2",
"uuidv4": "^6.2.12",
"winston": "^3.3.3"
},
"devDependencies": {
"#nestjs/cli": "^8.0.0",
"#nestjs/schematics": "^8.0.0",
"#nestjs/testing": "^8.0.0",
"#types/cookie-parser": "^1.4.2",
"#types/express": "^4.17.13",
"#types/jest": "^27.0.1",
"#types/lodash": "^4.14.178",
"#types/multer": "^1.4.7",
"#types/node": "^16.0.0",
"#types/nodemailer": "^6.4.4",
"#types/sequelize": "^4.28.10",
"#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": "^27.2.5",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}

Related

Build typescript/express application

The application functions smoothly during the development phase, however, it encounters difficulties when being built. The issue arises during the build process, causing imports errors in the the app.
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'dependency' imported from C:\Users\adria\root\media-hunters\coffee-reviews\be\dist\index.js
i'm getting errors like this one
package.json
{
"name": "coffee-reviews",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"type": "module",
"scripts": {
"build": "tsc -p tsconfig.json",
"serve": "npm run build && node dist/index.js",
"start": "ts-node -r tsconfig-paths/register src/index.ts",
"dev": "npm run db:migrate && nodemon --experimental-modules --es-module-specifier-resolution=node index.ts",
"format": "prettier **/*.ts --write",
"lint": "npx eslint --fix ./**/*.ts",
"db:seed": "ts-node -r tsconfig-paths/register -r dotenv/config node_modules/typeorm-seeding/dist/cli.js seed -n ormconfig.js -c default",
"db:reload": "ts-node src/infrastructure/database/cli/dbReload.ts reload",
"db:migrate": "ts-node -r tsconfig-paths/register node_modules\\typeorm\\cli.js migration:run",
"db:generate": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:generate -n",
"db:packet": "npm run db:reload && npm run db:migrate && npm run db:seed"
},
"lint-staged": {
"src/**/*.ts": [
"prettier --write",
"eslint --fix"
]
},
"dependencies": {
"#types/bcrypt": "^3.0.0",
"#types/cors": "^2.8.6",
"#types/express": "^4.17.13",
"#types/express-serve-static-core": "^4.17.30",
"#types/faker": "^4.1.9",
"#types/helmet": "^0.0.45",
"#types/jsonwebtoken": "^8.3.7",
"#types/method-override": "^0.0.31",
"#types/morgan": "^1.7.37",
"#types/node": "^13.5.0",
"#types/swagger-ui-express": "^4.1.1",
"#types/yargs": "^15.0.1",
"#wufe/mapper": "^0.5.0",
"#zerollup/ts-transform-paths": "^1.7.18",
"apollo-server-express": "^3.11.1",
"bcrypt": "^5.0.0",
"cors": "^2.8.5",
"debug": "~4.1.1",
"dotenv": "^8.2.0",
"express": "~4.17.1",
"graphql": "^15.8.0",
"graphql-tools": "^8.3.14",
"helmet": "^3.21.2",
"http-status-codes": "^1.4.0",
"inversify": "^6.0.1",
"inversify-express-utils": "^6.3.2",
"jsonwebtoken": "^9.0.0",
"method-override": "^3.0.0",
"morgan": "^1.9.1",
"path": "^0.12.7",
"pg": "^8.8.0",
"reflect-metadata": "^0.1.13",
"swagger-ui-express": "^4.1.3",
"ttypescript": "^1.5.15",
"type-graphql": "^1.1.1",
"typeorm": "^0.2.45",
"typeorm-seeding": "^1.6.1",
"typeorm-transactional-cls-hooked": "^0.1.20",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^4.7.1",
"yargs": "^17.6.2"
},
"devDependencies": {
"#types/chai": "^4.2.7",
"#types/sinon": "^7.5.1",
"#typescript-eslint/eslint-plugin": "^5.50.0",
"#typescript-eslint/parser": "^5.48.0",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"eslint": "^8.31.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^6.10.0",
"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-typescript-sort-keys": "^2.1.0",
"husky": "^3.1.0",
"lint-staged": "^7.3.0",
"nodemon": "^2.0.2",
"prettier": "^2.8.2",
"ts-node": "^10.9.1",
"tsconfig-paths": "^3.14.1",
"typescript": "^4.4.4"
},
"keywords": [],
"author": "",
"license": "ISC"
}
tsconfig.json
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"core/*": ["src/core/*"],
"dependency/*": ["src/dependency/*"],
"infrastructure/*": ["src/infrastructure/*"],
"ui/*": ["src/ui/*"],
"config/*": ["test/config/*"],
"*": [
"#types/*"
]
}
}
}
tsconfig.base.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "ESNext",
"module": "ESNext",
"outDir": "dist",
"strict": true,
"sourceMap": true,
"moduleResolution": "node",
"lib": [
"es5",
"es6",
"dom"
],
"plugins": [
{ "transform": "#zerollup/ts-transform-paths" }
],
"esModuleInterop": true
},
"include": [
"src/**/*.ts",
"tests/**/*.ts"
],
"exclude": [
"node_modules"
]
}
i have tried variety of combinations with commonjs and module version every of them end up quiet same, that the imports stop to work

Not being able to run NestJS application

I don't know what happened but I'm not being able to run a NestJS application locally
What I've tried so far:
deleting node_modules
deleting package-lock.json
npm i --save-dev #types/node
gave up and git cloned the whole application in a different folder (that runs on my colleagues' machines but not mine, they couldn't figure it out either)
I've been losing my mind, any help is appreciated
NPM Version 8.5.4
Node.js Version v16.14.2
tsconfig.json:
{
"compilerOptions": {
"lib": ["es2020"],
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"target": "es2020",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./src/",
"paths": {
"<rootDir>/": ["."]
},
"incremental": true,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"resolveJsonModule": true
}
}
tsconfig.build.json:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "__mocks__", "dist", "**/*spec.ts"]
}
package.json:
{
"name": "REDACTED",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"check-env": "node -r ts-node/register -r tsconfig-paths/register src/checkEnvs.ts",
"prebuild": "rimraf dist && npm run check-env",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node --max-old-space-size=2500 dist/src/main.js",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --detectOpenHandles",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:noCov": "jest --watch --collectCoverage=false",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"prisma:create": "npx prisma migrate dev --create-only",
"prisma:run": "npx prisma migrate dev",
"prisma:generate": "npx prisma generate"
},
"dependencies": {
"#elastic/elasticsearch": "7.10",
"#nestjs/bull": "^0.4.2",
"#nestjs/common": "^8.0.0",
"#nestjs/config": "^1.1.0",
"#nestjs/core": "^8.0.0",
"#nestjs/elasticsearch": "^8.0.0",
"#nestjs/event-emitter": "^1.0.0",
"#nestjs/microservices": "^8.1.2",
"#nestjs/mongoose": "^9.0.3",
"#nestjs/platform-express": "^8.4.4",
"#nestjs/schedule": "^1.1.0",
"#ntegral/nestjs-sentry": "^3.0.7",
"#prisma/client": "^3.14.0",
"#sentry/node": "^6.17.3",
"#sentry/types": "^6.17.3",
"amqp-connection-manager": "^3.7.0",
"amqplib": "^0.8.0",
"axios": "^0.24.0",
"bull": "^3.29.3",
"class-validator": "^0.13.2",
"cli-progress": "^3.10.0",
"date-fns": "^2.27.0",
"fast-csv": "^4.3.6",
"lodash": "^4.17.21",
"mongoose": "^6.2.10",
"nestjs-ftp": "^0.1.13",
"nestjs-soap": "^2.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"xml-js": "^1.6.11"
},
"devDependencies": {
"#golevelup/ts-jest": "^0.3.2",
"#nestjs/cli": "^8.0.0",
"#nestjs/schematics": "^8.0.0",
"#nestjs/testing": "^8.0.0",
"#types/bull": "^3.15.7",
"#types/cli-progress": "^3.9.2",
"#types/cron": "^1.7.3",
"#types/express": "^4.17.13",
"#types/jest": "^27.0.1",
"#types/lodash": "^4.14.179",
"#types/node": "^16.0.0",
"#types/sax": "^1.2.3",
"#types/supertest": "^2.0.11",
"#types/validator": "^13.7.2",
"#typescript-eslint/eslint-plugin": "^4.28.2",
"#typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.0.6",
"leaked-handles": "^5.2.0",
"mongodb-memory-server": "^8.4.2",
"prettier": "^2.3.2",
"prisma": "^3.14.0",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
}
}
If someone finds this question pulling their hair out, it turned out to have been my tsconfig.json file, all I had to do was roll back the lib version from "lib": ["es2020"] to "lib": ["es2017"],
I wish I had seen that on my crystal ball

TypeScript alias throws an error in production

I have a Node/Express application with TypeScript but somehow I get an alias error when I try to run the app in Digital Ocean droplet, although everything works fine in my local environment:
/VHF/.npm/_npx/112a5e3dd6e78d88/node_modules/tsc-alias/dist/index.js:60
yield (0, helpers_1.importReplacers)(config, replacers, options.replacers);
^
TypeError: (0 , helpers_1.importReplacers) is not a function
at /VHF/.npm/_npx/112a5e3dd6e78d88/node_modules/tsc-alias/dist/index.js:60:45
at Generator.next (<anonymous>)
at /VHF/.npm/_npx/112a5e3dd6e78d88/node_modules/tsc-alias/dist/index.js:8:71
at new Promise (<anonymous>)
at __awaiter (/VHF/.npm/_npx/112a5e3dd6e78d88/node_modules/tsc-alias/dist/index.js:4:12)
at replaceTscAliasPaths (/VHF/.npm/_npx/112a5e3dd6e78d88/node_modules/tsc-alias/dist/index.js:28:12)
at Object.<anonymous> (/VHF/.npm/_npx/112a5e3dd6e78d88/node_modules/tsc-alias/dist/bin/index.js:17:30)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
This is package.json:
{
"name": "vhf-digital-lending-api",
"version": "1.0.0",
"description": "API for VHF digital lender and payment platform",
"author": "",
"license": "ISC",
"scripts": {
"start": "npm run build&& cross-env; NODE_ENV=production node dist/server.js",
"dev": "cross-env NODE_ENV=development nodemon",
"build": "tsc && npx tsc-alias",
"test": "jest --forceExit --detectOpenHandles",
"lint": "eslint --ignore-path .gitignore --ext .ts src/",
"lint:fix": "npm run lint -- --fix",
"migrate:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate --config src/databases/index.ts -n",
"migration:run": "ts-node --transpile-only ./node_modules/typeorm/cli.js migration:run --config src/databases/index.ts",
"migration:revert": "ts-node --transpile-only ./node_modules/typeorm/cli.js migration:revert --config src/databases/index.ts"
},
"dependencies": {
"#types/cookie-session": "^2.0.44",
"#types/express-session": "^1.17.0",
"agenda": "^4.2.1",
"bcrypt": "^5.0.1",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"compression": "^1.7.4",
"config": "^3.3.6",
"cookie-parser": "^1.4.5",
"cookie-session": "^2.0.0",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"date-fns": "^2.24.0",
"dotenv": "^9.0.0",
"envalid": "^7.1.0",
"express": "^4.17.1",
"helmet": "^4.6.0",
"hpp": "^0.2.3",
"jest": "^26.6.3",
"joi": "^17.4.2",
"jsonwebtoken": "^8.5.1",
"mkdirp": "^1.0.4",
"morgan": "^1.10.0",
"multer": "^1.4.3",
"node-fetch": "^2.6.6",
"pg": "^8.7.1",
"reflect-metadata": "^0.1.13",
"swagger-jsdoc": "^6.1.0",
"swagger-ui-express": "^4.1.6",
"ts-jest": "^26.5.6",
"ts-node": "^9.1.1",
"typeorm": "^0.2.38",
"typeorm-pagination": "^2.0.3",
"typescript": "^4.4.4",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.4",
"xml2js": "^0.4.23"
},
"devDependencies": {
"#types/bcrypt": "^3.0.1",
"#types/compression": "^1.7.2",
"#types/config": "0.0.38",
"#types/cookie-parser": "^1.4.2",
"#types/cors": "^2.8.12",
"#types/dotenv": "^8.2.0",
"#types/express": "^4.17.13",
"#types/helmet": "^4.0.0",
"#types/hpp": "^0.2.2",
"#types/jest": "^26.0.24",
"#types/jsonwebtoken": "^8.5.5",
"#types/mongoose": "^5.11.97",
"#types/morgan": "^1.9.3",
"#types/node": "^15.14.9",
"#types/supertest": "^2.0.11",
"#types/swagger-jsdoc": "^6.0.1",
"#types/swagger-ui-express": "^4.1.3",
"#types/winston": "^2.4.4",
"#types/xml2js": "^0.4.9",
"#typescript-eslint/eslint-plugin": "^4.22.1",
"#typescript-eslint/parser": "^4.22.1",
"eslint": "^7.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"node-config": "0.0.2",
"node-gyp": "^8.3.0",
"nodemon": "^2.0.14",
"prettier": "^2.4.1",
"supertest": "^6.1.6",
"tsconfig-paths": "^3.11.0"
}
}
and this is my tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"target": "es2017",
"lib": ["es2017", "esnext.asynciterable"],
"typeRoots": ["node_modules/#types"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"declaration": true,
"outDir": "./dist",
"allowJs": true,
"noEmit": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"importHelpers": true,
"baseUrl": "src",
"paths": {
// "#/*": ["*"],
// "#controllers/*": ["controllers/*"],
// "#databases": ["databases"],
// "#dtos/*": ["dtos/*"],
// "#entity/*": ["entity/*"],
// "#exceptions/*": ["exceptions/*"],
// "#interfaces/*": ["interfaces/*"],
// "#middlewares/*": ["middlewares/*"],
// "#routes/*": ["routes/*"],
// "#services/*": ["services/*"],
// "#utils/*": ["utils/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
"exclude": ["node_modules"]
}
I tried commenting out the aliases but it doesn't seem to work.

SyntaxError: Unexpected token { import {MigrationInterface, QueryRunner} from "typeorm"

I am recently working with NestJs and typeORM but am getting an error when I run my nest application. It generates this error
/home/backend/src/migrations/1611061187746-loadAllEntities.ts:1
import { MigrationInterface, QueryRunner } from 'typeorm';
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
That error pointing at my generated migration ts file
//xxxxx-migration.ts
import {MigrationInterface, QueryRunner} from "typeorm";
export class migrationMessageDetail1611143681385 implements MigrationInterface {
name = 'migrationMessageDetail1611143681385'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("CREATE TABLE `detail_pricing` (`id` int NOT NULL AUTO_INCREMENT, `dayStart` int NOT NULL, `dayEnd` int NOT NULL, `isCommonChoice` tinyint NOT NULL, `price` int NOT NULL DEFAULT '200', `pricePerday` int NOT NULL, `discount` int NOT NULL, `currency` varchar(255) NOT NULL, `withDeposit` int NOT NULL DEFAULT '30', `noDeposit` varchar(255) NOT NULL DEFAULT '56', `carId` int NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB");
....
Here is the ormconfig.ts file
//ormconfig.ts
import { ConnectionOptions } from 'typeorm';
const connectionOptions: ConnectionOptions = {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'cave',
synchronize: false,
migrationsRun: true,
logging: ['warn', 'error'],
entities: ['dist/src/domain/persistence/entities/**/*.js'],
migrationsTableName: 'migrations',
migrations: ['src/migrations/*.ts'],
subscribers: ['src/subscriber/**/*.ts'],
cli: {
entitiesDir: 'src/domain/persistence/entities',
migrationsDir: 'src/migrations',
subscribersDir: 'src/subscriber',
},
};
export default connectionOptions;
and here is my config tsconfig file
//tsconfig.json
{
"compilerOptions": {
"lib": [
"es5",
"es6",
"es2020",
"esnext"
],
"resolveJsonModule": true,
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2020",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"esModuleInterop": true
}
}
I have tried to
change migrations: ['src/migrations/*.ts'], to [__dirname + 'migrations/*{.ts/.js}'], in ormconfig.ts, no Error when running the App, but migration:run won't work..
Here how I run and generate migrations
#generate migration
$ ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/configs/ormconfig.ts migration:generate -n migrationMessageDetail -d src/migrations
#run migration
$ ./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js --config src/configs/ormconfig.ts migration:run
here is my package json
{
"name": "cave-backend",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"#golevelup/nestjs-rabbitmq": "^1.15.2",
"#nestjs/common": "^7.5.5",
"#nestjs/config": "^0.6.1",
"#nestjs/core": "^7.5.5",
"#nestjs/mapped-types": "^0.1.1",
"#nestjs/passport": "^7.1.5",
"#nestjs/platform-express": "^7.5.5",
"#nestjs/platform-fastify": "^7.5.5",
"#nestjs/swagger": "^4.7.5",
"#nestjs/typeorm": "^7.1.5",
"#turf/distance": "^6.0.1",
"bcrypt": "^5.0.0",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
"dotenv": "^8.2.0",
"fastify-helmet": "^5.0.3",
"fastify-swagger": "^3.5.0",
"jwks-rsa": "^1.12.0",
"md5": "^2.3.0",
"mysql": "^2.18.1",
"nexmo": "^2.9.1",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"swagger-ui-express": "^4.1.5",
"typeorm": "^0.2.29",
"uuid": "^8.3.1"
},
"devDependencies": {
"#nestjs/cli": "^7.5.3",
"#nestjs/schematics": "^7.2.2",
"#nestjs/testing": "^7.5.5",
"#types/cache-manager": "^2.10.3",
"#types/express": "^4.17.9",
"#types/jest": "^26.0.15",
"#types/node": "^14.14.10",
"#types/supertest": "^2.0.10",
"#typescript-eslint/eslint-plugin": "^4.8.2",
"#typescript-eslint/parser": "^4.8.2",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.0.1",
"ts-jest": "^26.4.4",
"ts-loader": "^8.0.11",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.2"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
I could generate and run the migration but I don't know why this error SyntaxError: Unexpected token { import {MigrationInterface, QueryRunner} from "typeorm" occurs
Please help, Thank you so much
Have you tried to change this:
migrations: ['src/migrations/*.ts'],
to this:
migrations: ['src/migrations/*{.ts,.js}'],
?

Cors pref-light request seem not to work nestjs

I tried enabling nestjs cors with no success
here are various approaches that never worked.
It seems nestjs does not respond to OPTION request with anything even after below config.
Tried solutions
https://github.com/nestjs/nest/issues/1134#issuecomment-435902027
https://github.com/nestjs/nest/issues/1134#issuecomment-435893861
https://github.com/nestjs/nest/issues/1134#issuecomment-477531712
import { AppModule } from './app/app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule,{ cors: true });
const options = {
"origin":"http://localhost:3000", // attempted "origin":["http://localhost"]
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204,
"credentials":true,
"allowedHeaders": "Content-Type, Accept,Authorization",
}
/**
* {
origin: [
/^(.*)/,
],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 200,
credentials: true,
allowedHeaders:
'Origin,X-Requested-With,Content-Type,Accept,Authorization,authorization,X-Forwarded-for',
}
*/
app.enableCors(options)
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Accept,Authorization');
next();
});
await app.listen(3000);
}
bootstrap();```
----------
**package.json**
```
{
"name": "moat-backend",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"#nestjs/common": "^7.0.0",
"#nestjs/core": "^7.0.0",
"#nestjs/platform-express": "^7.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.5.4",
"#nestjs/bull": "^0.1.1",
"#nestjs/jwt": "^7.0.0",
"#nestjs/passport": "^7.0.0",
"#nestjs/swagger": "^4.5.5",
"#nestjs/testing": "^7.0.9",
"#nestjs/typeorm": "^7.0.0",
"#types/bcrypt": "^3.0.0",
"#types/dotenv": "^8.2.0",
"#types/email-templates": "^7.0.1",
"#types/nodemailer": "^6.4.0",
"#types/passport": "^1.0.3",
"bcrypt": "^4.0.1",
"bcryptjs": "^2.4.3",
"bull": "^3.13.0",
"class-transformer": "^0.2.3",
"class-validator": "^0.12.2",
"crypto-js": "^4.0.0",
"dotenv": "^8.2.0",
"ejs": "^3.1.2",
"email-templates": "^7.0.4",
"fs-extra": "^9.0.0",
"hash-anything": "^1.2.2",
"hat": "^0.0.3",
"keygenerator": "^1.0.4",
"mysql": "^2.18.1",
"nodemailer": "^6.4.6",
"nodemon": "^2.0.3",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"pug": "^2.0.4",
"sha256": "^0.2.0",
"swagger-ui-express": "^4.1.4",
"typeorm": "^0.2.24",
"uuid": "^7.0.3"
},
"devDependencies": {
"#nestjs/cli": "^7.0.0",
"#nestjs/schematics": "^7.0.0",
"#nestjs/testing": "^7.0.9",
"#types/express": "^4.17.3",
"#types/jest": "25.1.4",
"#types/node": "^13.9.1",
"#types/supertest": "^2.0.8",
"#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",
"jest": "^25.1.0",
"prettier": "^1.19.1",
"supertest": "^4.0.2",
"ts-jest": "25.2.1",
"ts-loader": "^6.2.1",
"ts-node": "^8.6.2",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
Browser Tested on
Firefox 64.0
chrome Version 75.0.3770.90 (Official Build) (64-bit)
Platform
Linux Mint 19
Objective
Consume Nest Api with React
i got the way to have it work i used cors package and hocked it to nest app
const app = await NestFactory.create(AppModule,{ cors: true });
const options = {
"origin":true, // attempted "origin":["http://localhost"]
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 200,
"credentials":true,
"allowedHeaders": "Content-Type, Accept,Authorization",
}
app.use(cor(options))
await app.listen(3000);
}

Resources