a7urag / node-express-mysql-typescript-api-boilerplate

Boilerplate for node express mysql typescript api project
191 stars 60 forks source link

`yarn serve` returns an error if using `dist/index.js` #10

Open GeorgeKanchev opened 4 years ago

GeorgeKanchev commented 4 years ago

There's no dist folder so I ran yarn build, dist was generated.

If I run yarn serve, I get this error:

yarn run v1.19.0 $ node dist/index.js INFO: All classes found using provided glob pattern "/.entity.ts" : "src/entities/user/user.entity.ts" {"message":"Database connection failed with error SyntaxError: Cannot use import statement outside a module","level":"info","timestamp":"2020-07-27T15:23:18.793Z"} Done in 1.11s. If instead I run nodemon index.ts (so not in dist folder), everything runs normally.

Is there an issue with the build process, or maybe something I don't see? Readme mention build-ts, but that doesn't link to anywhere, so assuming it's build now.

zaratt commented 3 years ago

Hello, As I understand it, in package.json you must include "type": "module", as shown below. `"migration:run": "ts-node ./node_modules/.bin/typeorm migration:run" },

"type": "module",

"keywords": [......`

But for me another problem arose. If you know how to solve it, thank you very much. Problem: _ExperimentalWarning: The ESM module loader is experimental. file:///............/GitHub/node-express-mysql-typescript-api-boilerplate/dist/index.js:5 exports.__esModule = true; ^

ReferenceError: exports is not defined_

kunal-dd commented 3 years ago

@zaratt

Which version of node you are using?

zaratt commented 3 years ago

Node 14.15.4

kunal-dd commented 3 years ago

@zaratt

Remove type: module from package.json and user only .js in ormconfig.js

My ormconfig.js is as below:

module.exports = { type: 'mysql', host: process.env.DB_HOST, username: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, synchronize: process.env.NODE_ENV !== 'prod', entities: [ __dirname + '/dist/entities/**/*.js'], logging: process.env.NODE_ENV !== 'production' ? 'all' : 'error', migrations: [__dirname + '/dist/migration/**/*.js'], subscribers: [__dirname + '/dist/subscriber/**/*.js'], cli: { migrationsDir: "migration" }, connectTimeout: 30000, acquireTimeout: 30000 }