запустить tsc: ошибка [ERR_MODULE_NOT_FOUND]: не удается найти модуль

#javascript #typescript #tsc

#javascript #машинописный текст #tsc

Вопрос:

У меня ошибка при использовании yarn tsc и узла —harmony-top-level-await dist / index.js

мой package.json:

 {
  "name": "micro-hr",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "commit": "git-cz",
    "build": "babel src --plugins @babel/plugin-syntax-top-level-await --extensions ".js,.ts" --out-dir dist --copy-files --no-copy-ignored",
    "start:dev": "ts-node-dev --experimental-modules --inspect --respawn --transpile-only --ignore-watch node_modules -r tsconfig-paths/register src/index.ts",
    "node:dev": "nodemon --exec node --loader ts-node/esm.mjs --experimental-specifier-resolution=node --experimental-top-level-await -r tsconfig-paths/register --transpile-only  --no-warnings ./src/index.ts",
    "start:debug": "node start --debug --watch",
    "start:prod": "node --harmony-top-level-await dist/index.js",
    "test": "jest",
    "lint": "eslint --fix",
    "lint2": "eslint "{src,apps,libs,test}/**/*.ts" --fix",
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config ./ormconfig.ts",
    "migration:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate -n"
  },
  "devDependencies": {
    "@babel/cli": "^7.11.5",
    "@babel/core": "^7.11.6",
    "@babel/node": "^7.10.5",
    "@babel/plugin-syntax-top-level-await": "^7.10.4",
    "@babel/preset-env": "^7.11.5",
    "@babel/preset-typescript": "^7.10.4",
    "@commitlint/cli": "^9.1.2",
    "@commitlint/config-conventional": "^9.1.2",
    "@types/bcryptjs": "^2.4.2",
    "@types/cookie-parser": "^1.4.2",
    "@types/cors": "^2.8.7",
    "@types/express": "^4.17.8",
    "@types/helmet": "^0.0.48",
    "@types/jest": "^26.0.13",
    "@types/node": "^14.6.4",
    "@types/pino": "^6.3.0",
    "@types/pino-http": "^5.0.5",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.0.1",
    "@typescript-eslint/parser": "^4.0.1",
    "babel-plugin-module-resolver": "^4.0.0",
    "commitizen": "^4.2.1",
    "cz-conventional-changelog": "^3.3.0",
    "eslint": "^7.8.1",
    "eslint-config-airbnb-base": "^14.2.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-config-standard": "^14.1.1",
    "eslint-import-resolver-typescript": "^2.3.0",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^3.1.4",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1",
    "husky": "^4.2.5",
    "jest": "^26.4.2",
    "nodemon": "^2.0.4",
    "pino-pretty": "^4.2.0",
    "prettier": "^2.1.1",
    "supertest": "^4.0.2",
    "ts-jest": "^26.3.0",
    "ts-node": "^9.0.0",
    "ts-node-dev": "^1.0.0-pre.62",
    "tsconfig-paths": "^3.9.0",
    "tscpaths": "^0.0.9",
    "typescript": "^4.0.2"
  },
  "dependencies": {
    "amqplib": "^0.6.0",
    "assert": "^2.0.0",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.3.1",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "envalid": "^6.0.2",
    "express": "^4.17.1",
    "extendable-error": "^0.1.7",
    "helmet": "^4.1.0",
    "nabbitmq": "^1.0.0",
    "pg": "^8.3.3",
    "pino": "^6.5.1",
    "pino-http": "^5.2.0",
    "reflect-metadata": "^0.1.13",
    "spt-rabbit-helpers": "^1.0.6",
    "tsyringe": "^4.3.0",
    "typeorm": "^0.2.25"
  },
  "config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }
}
  

tsconfig.json:

 {
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "strictPropertyInitialization": false,
    "moduleResolution": "node",
    "baseUrl": "./src",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitAny": true,
    "typeRoots": ["node_modules/@types", "@types"]
  },
  "include": ["src", "__tests__"],
  "exclude": ["node_modules"]
}
  

index.ts:

 import 'reflect-metadata';
import express from 'express';
import validateEnv from './utils/validateEnvs';
import nabbitmq from 'nabbitmq';
import tsyringe from 'tsyringe';
const factory = new nabbitmq.RabbitMqConnectionFactory();
factory.setUri('amqp://localhost:5672');
const ConsumerConnection = await factory.newConnection();
const PublisherConnection = await factory.newConnection();
tsyringe.container.register<nabbitmq.RabbitMqConnection>('ConsumerConnection', {
  useValue: ConsumerConnection,
});
tsyringe.container.register<nabbitmq.RabbitMqConnection>(
  'PublisherConnection',
  {
    useValue: PublisherConnection,
  },
);
// container.registerInstance('Consumer_Connection', ConsumerConnection);
// container.registerInstance('Publisher_Connection', PublisherConnection);
// const test = container.resolve('Consumer_Connection');
// console.log(test === ConsumerConnection);
const app = express();
validateEnv();
  

моя функция validateEnv:

 import envalid from 'envalid';
import dotenv from 'dotenv';
const { cleanEnv, port, str, host } = envalid;
function validateEnv() {
  dotenv.config();
  cleanEnv(process.env, {
    NODE_ENV: str(),
    DB_HOST: host(),
    DB_NAME: str(),
    DB_USER: str(),
    DB_PASS: str(),
    DB_PORT: port(),
    SERVER_PORT: port(),
    RABBIT_HOST: str(),
  });
}

export default validateEnv;
 
  

я получил эту ошибку из моего перенесенного кода:

 Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/spirit/Documentos/microservices_emasa/backend/micro-hr/dist/utils/validateEnvs' imported from /home/spirit/Documentos/microservices_emasa/backend/micro-hr/dist/index.js
  

но по какой-то причине я получаю ошибку при использовании этой команды для запуска сервера:

узел —гармония-верхний уровень-ожидает dist / index.js

это мой перенесенный код:

index.js

 import 'reflect-metadata';
import express from 'express';
import validateEnv from './utils/validateEnvs';
import nabbitmq from 'nabbitmq';
import tsyringe from 'tsyringe';
const factory = new nabbitmq.RabbitMqConnectionFactory();
factory.setUri('amqp://localhost:5672');
const ConsumerConnection = await factory.newConnection();
const PublisherConnection = await factory.newConnection();
tsyringe.container.register('ConsumerConnection', {
  useValue: ConsumerConnection,
});
tsyringe.container.register('PublisherConnection', {
  useValue: PublisherConnection,
});
const app = express();
validateEnv();
  

validateEnvs.js:

 import envalid from 'envalid';
import dotenv from 'dotenv';
const { cleanEnv, port, str, host } = envalid;
function validateEnv() {
    dotenv.config();
    cleanEnv(process.env, {
        NODE_ENV: str(),
        DB_HOST: host(),
        DB_NAME: str(),
        DB_USER: str(),
        DB_PASS: str(),
        DB_PORT: port(),
        SERVER_PORT: port(),
        RABBIT_HOST: str(),
    });
}
export default validateEnv;
  

Я не знаю, почему это происходит