Run script compiled using typescript + Babel + typeorm

Asked

Viewed 78 times

-1

While trying to execute node .\dist\index.js compiled using Babel have the following error:

inserir a descrição da imagem aqui

In dev runs without errors, all tests also pass, consuming the API also everything normal, but when I build with Babel, the build is generated successfully and when running node .\dist\index.js have this problem.

Any hint of what I’m doing wrong?

My Abel.config:

    module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current'
                }
            }
        ],
        '@babel/preset-typescript'
    ],
    plugins: [
        "@babel/plugin-transform-async-to-generator",
        "babel-plugin-transform-typescript-metadata",
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        ["@babel/plugin-proposal-class-properties", { "loose": true }],
        [
            "@babel/plugin-transform-runtime",
            {
                "helpers": true,
                "regenerator": true
            }
        ]
    ],
    ignore: [
        '**/*.spec.ts'
    ]
}

My tsconfig:

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "lib": [
      "ES6"
    ],
    "allowJs": true,
    "rootDir": ".",
    "outDir": "./dist",
    "strict": false,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_module/@types",
      "./src/@types"
    ],
    "types": [
      "@types/jest"
    ],
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "./src"
  },
  "include": [
    "src/**/*"
  ]
}

My package:

{
  "name": "backend",
  "version": "1.0.0",
  "main": "index.js",
  "license": "ISC",
  "scripts": {
    "start": "node dist/index.js",
    "dev": "ts-node-dev -r tsconfig-paths/register --inspect --transpile-only --ignore node_modules src",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
    "test": "cross-env NODE_ENV=test DB_DEFAULT=test jest --detectOpenHandles",
    "build": "rimraf ./dist && babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored"
  },
  "devDependencies": {
    "@babel/cli": "^7.13.10",
    "@babel/core": "^7.13.10",
    "@babel/plugin-proposal-class-properties": "^7.13.0",
    "@babel/plugin-proposal-decorators": "^7.13.5",
    "@babel/plugin-transform-async-to-generator": "^7.13.0",
    "@babel/plugin-transform-runtime": "^7.13.10",
    "@babel/preset-env": "^7.13.10",
    "@babel/preset-typescript": "^7.13.0",
    "@types/bcrypt": "^3.0.0",
    "@types/cors": "^2.8.10",
    "@types/express": "^4.17.11",
    "@types/express-boom": "^3.0.0",
    "@types/express-validator": "^3.0.0",
    "@types/jest": "^26.0.20",
    "@types/jsonwebtoken": "^8.5.0",
    "@types/morgan": "^1.9.2",
    "@types/node": "^14.14.31",
    "@types/nodemailer": "^6.4.0",
    "@types/rimraf": "^3.0.0",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.17.0",
    "@typescript-eslint/parser": "^4.17.0",
    "babel-plugin-transform-typescript-metadata": "^0.3.1",
    "cross-env": "^7.0.3",
    "eslint": "^7.21.0",
    "eslint-config-standard": "^16.0.2",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-promise": "4.2.1",
    "jest": "^26.6.3",
    "rimraf": "^3.0.2",
    "sqlite3": "^5.0.2",
    "supertest": "^6.1.3",
    "ts-jest": "^26.5.3",
    "ts-node-dev": "^1.1.6",
    "typescript": "^4.2.2"
  },
  "dependencies": {
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-boom": "^3.0.0",
    "express-validator": "^6.10.0",
    "generate-password": "^1.6.0",
    "helmet": "^4.4.1",
    "jsonwebtoken": "^8.5.1",
    "morgan": "^1.10.0",
    "mssql": "^6.3.1",
    "mysql2": "^2.2.5",
    "nodemailer": "^6.5.0",
    "typeorm": "^0.2.31"
  }
}
  • I believe this problem is caused by tsconfig-paths. Do you use any alias in the project?

  • No.... I thought that too and removed all...the project now this without any alias....

  • I added "moduleResolution": "node" to tsconfig, still same error :-(

1 answer

0

Example of how is getting the User.ts tanspilado by Babel:

 "use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.User = void 0;

var _initializerDefineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerDefineProperty"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor"));

var _initializerWarningHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerWarningHelper"));

var _bcrypt = _interopRequireDefault(require("bcrypt"));

var _typeorm = require("typeorm");

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _dec14, _dec15, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _temp;

let User = (_dec = (0, _typeorm.Entity)('users'), _dec2 = Reflect.metadata("design:type", Function), _dec3 = Reflect.metadata("design:paramtypes", [Number, String, String, String]), _dec4 = (0, _typeorm.PrimaryGeneratedColumn)('increment'), _dec5 = Reflect.metadata("design:type", Number), _dec6 = (0, _typeorm.Column)(), _dec7 = Reflect.metadata("design:type", String), _dec8 = (0, _typeorm.Column)(), _dec9 = Reflect.metadata("design:type", String), _dec10 = (0, _typeorm.Column)(), _dec11 = Reflect.metadata("design:type", String), _dec12 = (0, _typeorm.BeforeInsert)(), _dec13 = (0, _typeorm.BeforeUpdate)(), _dec14 = Reflect.metadata("design:type", Function), _dec15 = Reflect.metadata("design:paramtypes", []), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = (_temp = class User {
  /* @CreateDateColumn()
  created_at: Date;
    @UpdateDateColumn()
  updated_at: Date; */
  hashPassword() {
    var _this = this;

    return (0, _asyncToGenerator2.default)(function* () {
      if (_this.password) _this.password = yield _bcrypt.default.hash(_this.password, 10);
    })();
  }

  constructor(id, name, email, password) {
    (0, _initializerDefineProperty2.default)(this, "id", _descriptor, this);
    (0, _initializerDefineProperty2.default)(this, "name", _descriptor2, this);
    (0, _initializerDefineProperty2.default)(this, "email", _descriptor3, this);
    (0, _initializerDefineProperty2.default)(this, "password", _descriptor4, this);
    this.id = id;
    this.name = name;
    this.email = email;
    this.password = password;
  }

}, _temp), (_descriptor = (0, _applyDecoratedDescriptor2.default)(_class2.prototype, "id", [_dec4, _dec5], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor2 = (0, _applyDecoratedDescriptor2.default)(_class2.prototype, "name", [_dec6, _dec7], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor3 = (0, _applyDecoratedDescriptor2.default)(_class2.prototype, "email", [_dec8, _dec9], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), _descriptor4 = (0, _applyDecoratedDescriptor2.default)(_class2.prototype, "password", [_dec10, _dec11], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: null
}), (0, _applyDecoratedDescriptor2.default)(_class2.prototype, "hashPassword", [_dec12, _dec13, _dec14, _dec15], Object.getOwnPropertyDescriptor(_class2.prototype, "hashPassword"), _class2.prototype)), _class2)) || _class) || _class) || _class);
exports.User = User;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.