1

I created the build of my project but when I try to run it from within my dist folder this error appears. I'm using node version 10.19.0 and typescript 4.3.5

node dist/shared/infra/http/server.js

Server is running at http://localhost:3333

(node:5648) UnhandledPromiseRejectionWarning: /home/juniormanzini/estudos/firstNode/appointments/src/modules/notifications/infra/typeorm/schemas/Notification.ts:1
import {
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /home/juniormanzini/estudos/firstNode/appointments/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:39
    at Array.map (<anonymous>)
    at Object.importClassesFromDirectories (/home/juniormanzini/estudos/firstNode/appointments/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:10)
(node:5648) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5648) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It seems to me that this is the aforementioned problematic file.

import {
  Entity,
  ObjectID,
  ObjectIdColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity('notifications')
class Notification {
  @ObjectIdColumn()
  id: ObjectID;

  @Column()
  content: string;

  @Column('uuid')
  recipient_id: string;

  @Column({ default: false })
  read: boolean;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}

export default Notification;

Server.ts file

import 'reflect-metadata';
import 'dotenv/config';
import express from 'express';
import { errors } from 'celebrate';
import 'express-async-errors';
import cors from 'cors';
import helmet from 'helmet';

import '@shared/infra/typeorm';
import '@shared/container';

import uploadConfig from '@config/upload';
import globalExceptionHandler from '@shared/infra/http/middlewares/globalExceptionHandler';
import rateLimiter from '@shared/infra/http/middlewares/rateLimiter';
import routes from './routes';

const app = express();

app.use(helmet());
app.use(cors());
app.use(express.json());
app.use('/files', express.static(uploadConfig.uploadsFolder));
app.use(rateLimiter);
app.use(routes);
app.use(errors());
app.use(globalExceptionHandler);

app.listen(3333, () =>
  // eslint-disable-next-line
  console.log(' Server is running at http://localhost:3333'),
);

when I start the project using 'yarn start' it works as expected

Manzini
  • 1,671
  • 2
  • 9
  • 15

1 Answers1

1

When running this single file, Node.js doesn't know you are trying to run a part of a bigger module. Such import keyword only works with a project and { "type": "module" } in a package.json file.

As said here, just try to add this in your package.json:

"type": "module"

If it doesn't work, you can either try to upgrade your Node.js version or run Node.js with the --experimental-modules option in your command line:

node --experimental-modules dist/shared/infra/http/server.js
Drarig29
  • 1,902
  • 1
  • 19
  • 42