Eu tenho a seguinte configuração:
Package.json:
"scripts": {
"start": "node dist/server",
"build": "babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored",
"dev:server": "ts-node-dev --inspect --transpile-only --ignore node_modules src/server.ts",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
},
tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"allowJs": true,
// "outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"noImplicitAny": false,
"strictPropertyInitialization": false,
"moduleResolution": "node",
"typeRoots": [
"./node_modules/@types",
".src/@types"
],
"esModuleInterop": true,
"resolveJsonModule": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"@middlewares/*": [
"./src/middlewares/*"
],
}
},
"include": [
"src/**/*"
]
}
babel.config.js:
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
],
'@babel/preset-typescript'
],
plugins: [
['module-resolver', {
alias: {
'@config': './src/config',
'@database': './src/database',
'@helpers': './src/helpers',
'@middlewares': './src/middlewares',
'@models': './src/models',
'@routes': './src/routes',
'@schemas': './src/schemas',
'@services': './src/services',
}
}],
'babel-plugin-transform-typescript-metadata',
["@babel/plugin-proposal-decorators", { "legacy": true }],
],
ignore: ['**/*.spec.ts'],
}
Após gerar a build da minha API, é retornado o seguinte erro:
(node:21680) UnhandledPromiseRejectionWarning: C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\src\models\Bankroll.ts:2
import {
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Function.PlatformTools.load (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\typeorm\platform\PlatformTools.js:109:28)
at C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:39:69
at Array.map (<anonymous>)
at Object.importClassesFromDirectories (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:39:10)
at ConnectionMetadataBuilder.buildEntityMetadatas (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\typeorm\connection\ConnectionMetadataBuilder.js:50:97)
at Connection.buildMetadatas (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\typeorm\connection\Connection.js:495:57)
at Connection.<anonymous> (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\typeorm\connection\Connection.js:127:30)
at step (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\tslib\tslib.js:141:27)
at Object.next (C:\Users\Lucas\Documents\Projetos\Bets\bets - backend\node_modules\tslib\tslib.js:122:57)
Eu não consegui entender o motivo do erro, uma vez que ele está tentando executar um arquivo ts após a transpilação pelo babel (com o babel tendo gerado todos os arquivos .js sem problema).