Running "node" on /dist folder file errors because it sees /migrations and /src folders as well

65 views Asked by At

I have a node project written in typescript that I compile with tsc. It compiles to a /dist folder and then I run the output json with the command node dist/startup/server.js. My folder structure is as follows:

/node_modules
    ...
/src
    /foo
        a.ts
        b.ts
    /startup
        server.ts
/migrations
    1.ts
    2.ts
    ...

/dist (directory is created after tsc command completes)
    /foo
        ...
    /startup
        server.js

The problem is when I run node dist/startup/server.js (which should be "standalone" with node_modules) I get the error:

.../src/db/entities/appUser.ts:1
import { Entity, PrimaryColumn, Column, Index } from 'typeorm';
^^^^^^

SyntaxError: Cannot use import statement outside a module

I found that the error only occurs if I have the ./src and ./migrations directories. If I delete them, the server runs correctly.

I also tried to run the app from WITHIN the /dist folder, but that gives me another error so I believe that it means that the build expects to be run from root and not /dist (even if I copy node_modules into dist):

EntityMetadataNotFoundError: No metadata for "Tenant" was found.

Does anyone know how to make node not pick up the src and migrations folders? Or to configure tsc to be able to run within dist?

I expect that running node on a file in the /dist folder only reads the files necessary for that program to run in that directory, and not sibling directories /src /migrations.

I also expect to be able to run the code the same if I am in root or in /dist, but I believe that is a tsc issue.

0

There are 0 answers