-2

Ao tentar executar node .\dist\index.js compilado usando babel tenho o seguinte erro:

inserir a descrição da imagem aqui

Em dev roda sem erros, todos os tests tambem passam, consumindo a API tambem tudo normal, mas quando realizo o build com babel, o build é gerado com sucesso e ao executar node .\dist\index.js tenho este problema.

Alguma dica do que estou fazendo errado?

Meu babel.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'
    ]
}

Meu 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/**/*"
  ]
}

Meu 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"
  }
}
3
  • Creio que esse problema é causado pelo tsconfig-paths. Voce usa algum alias no projeto? Commented 14/03/2021 às 21:12
  • Não nenhum....pensei isso também e removi todos...o projeto agora esta sem nenhum alias...mesmo assim nada.
    – fabcardo
    Commented 14/03/2021 às 21:56
  • Adicionei "moduleResolution": "node" ao tsconfig, continua mesmo erro :-(
    – fabcardo
    Commented 14/03/2021 às 22:15

2 Respostas 2

-1

Exemplo de como esta ficando o User.ts tanspilado pelo 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;
-2
{
    "plugins": [
        "babel-plugin-transform-typescript-metadata",
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        ["@babel/plugin-proposal-private-property-in-object", { "loose": true }],
        ["@babel/plugin-proposal-class-properties", { "loose" : true }],
        ["module-resolver", {"alias": {}}]
    ],
    "presets": [
        ["@babel/preset-env", {"targets": {"node": "current"}}],
        "@babel/preset-typescript"
    ],
    "ignore": ["**/*.spec.ts"]
}


O arquivo .babelrc deste modo funciona com typescript e typeorm 

e o tsconfig.json 

```json
{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["es6"],
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "removeComments": true,
    "typeRoots": ["./node_modules/@types"],
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {},
    "moduleResolution": "node"
  },
  "include": ["src/**/*"],
  "exclude": [".vscode","node_modules","dist"]
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .