node.js - Oluşturulmuş TypeScript uygulaması import'ları yanlış klasörden alıyor, dist yerine src klasöründen


Özet; Uygulama derleniyor ama dist klasörü yerine src klasöründen dosya ithal etmeye çalışıyor

TypeScript ile oluşturulmuş bir Express uygulamam var.

Bu tsconfig.json dosyasıdır

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "sourceMap": true,                        /* Generates corresponding '.map' file. */
    "outDir": "dist",                         /* Redirect output structure to the directory. */
    "strict": false,                          /* Enable all strict type-checking options. */
    "noImplicitAny": false,                   /* Raise error on expressions and declarations with an implied 'any' type. */
    "moduleResolution": "node",               /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "baseUrl": "./dist",                      /* Base directory to resolve non-absolute module names. */
    "paths": {
      "*": [
        "node_modules/*"
      ]
    },                                        /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "experimentalDecorators": true,           /* Enables experimental support for ES7 decorators. */
    "emitDecoratorMetadata": true,            /* Enables experimental support for emitting type metadata for decorators. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "include": [
    "src/**/*",
    "custom.d.ts"
  ]
}

Bunlar benim npm komutlarım:

"scripts": {
      "clean": "rimraf dist/*",
      "dev": "nodemon",
      "tsc": "tsc",
      "start": "node dist/index.js",
      "build": "npm-run-all clean tsc",
   },

npm run build sorunsuz çalışıyor ve her şey gayet güzel görünüyor, ama npm run start çalıştırdığımda aşağıdaki hatayı alıyorum:

/app/server/src/entity/Category.ts:1
import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, BaseEntity} from "typeorm";
^^^^^^

SyntaxError: Cannot use import statement outside a module

Hata, benim derlememin bir şekilde src klasöründen ve değil de build klasöründen import yapıyor gibi görünmesi gibi görünüyor. Neyin yanlış olduğunu hiç bilmiyorum, daha ileri gitmeme yardımcı olacak herhangi bir rehberlik veya fikir takdir edilir.

Cevap #1

Tamam, yani "start": "node build/index.js" yaparsam yukarıdaki davranışla sonuçlanıyor, ancak komut satırında şu işlemi yaparsam:

cd build
node index.js

İşe yarıyor. Yani sanırım uygulamadaki bazı modüllerde bazı yollar yanlış. NPM scriptleri ile bunu çalıştırmanın iyi bir yolunu bulursam daha fazla bilgi paylaşırım.

Cevap #2

Typescript + TypeORM ile bu sorunla karşılaşan başka herkes için, ben de buna rastladım. Benim çözümüm, TypeORM yapılandırmamda yolları çözmeye yardımcı olmak için __dirname kullanmaktı.

Örneğin, yapmak yerine

const connection: DataSourceOptions = {
  ...
  entities: ['src/entities/*.ts', 'dist/entities/*{.ts,.js}'],
}

Bunu değiştirdim

const connection: DataSourceOptions = {
  ...
  entities: [`${__dirname}/../../entities/*{.ts,.js}`],
  migrations: [`${__dirname}/migrations/*{.ts,.js}`],
}

Umarım bu yardımcı olur!