Cannot use import statement Outside a module - Typescript and Babel

Asked

Viewed 2,220 times

-1

I have the following setup:

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'],
}

After generating my API build, the following error is returned:

(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)

I couldn’t understand the reason for the error since it is trying to run a ts file after transposing by Babel (with Babel having generated all the files. js without problem).

  • Try to include a "type" in package.json: "module"

1 answer

0

HI, I had this problem, in my ormconfig it points to src switched to dist and it worked. show example:

"entities": [ "dist/models//*.js" ], "Migrations": [ "dist/Migrations//*.js" ],

Browser other questions tagged

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