Reputation: 860
TLDR; App builds but tries to import files from src folder instead of dist
I have an Express app that is built with TypeScript.
This is the tsconfig.json
{
"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"
]
}
These are my npm scripts:
"scripts": {
"clean": "rimraf dist/*",
"dev": "nodemon",
"tsc": "tsc",
"start": "node dist/index.js",
"build": "npm-run-all clean tsc",
},
npm run build
works fine and all seems nice and dandy, but when I run npm run start
I get the following error:
/app/server/src/entity/Category.ts:1
import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, BaseEntity} from "typeorm";
^^^^^^
SyntaxError: Cannot use import statement outside a module
The error seems to be that my build for some reason seems to import from the src-folder and not from the build folder. I have no idea what is wrong, any guiding or ideas are appreciated to help me get further.
Upvotes: 2
Views: 1608
Reputation: 370
For anybody else that encounters this problem with Typescript + TypeORM, I encountered it too. The way I solved it was by using __dirname
to help resolve paths in my TypeORM config.
For example, instead of doing
const connection: DataSourceOptions = {
...
entities: ['src/entities/*.ts', 'dist/entities/*{.ts,.js}'],
}
I modified it to
const connection: DataSourceOptions = {
...
entities: [`${__dirname}/../../entities/*{.ts,.js}`],
migrations: [`${__dirname}/migrations/*{.ts,.js}`],
}
Hope this helps!
Upvotes: 1
Reputation: 860
Ok, so doing "start": "node build/index.js"
results in the above behaviour, however if I do the following on the command line:
cd build
node index.js
It works. So I guess some path is wrong in some module in the application. Will update with more if I find a good way to get this working with NPM scripts.
Upvotes: 1