So, I'm trying to start a new project but I keep running into the following error:
(Because somehow I can't embed images yet) The error: SyntaxError: Cannot use import statement outside a module
Common solutions I've found, like editing the tsconfig.json or adding "type":"modules" to the package.json have done nothing to help me out.
Here goes my package.json:
{
"name": "ihanduapp_v2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^16.10.2",
"nodemon": "^2.0.13",
"ts-node": "^10.2.1",
"typescript": "^4.4.3"
},
"dependencies": {
"class-validator": "^0.13.1",
"express": "^4.17.1",
"pg": "^8.7.1",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.37"
}
}
... and my tsconfig.json file as well:
{
"compilerOptions": {
"target": "es2019",
"moduleResolution": "node",
"module": "commonjs",
"lib": ["es2019"],
"sourceMap": true,
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"resolveJsonModule": true,
"alwaysStrict": true,
"removeComments": true,
"noImplicitReturns": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"paths": {
"@src/*": ["./src/*"],
"@test/*": ["./test/*"]
},
"rootDirs": ["./src", "./test"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": ["./src/**/*.ts", "./test/**/*.ts"],
"exclude": ["./node_modules/*", "dist"]
}
I have no idea what to do, especially because I can't seem to find how to make TypeORM work with the "require" syntax, as a way to just go around the problem... Any help is appreciated.
Thanks in advance.
Edit 1:
The file which is causing me problems is located at: src/models/User.ts and is as follows:
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
} from "typeorm";
import { MaxLength, MinLength } from "class-validator";
@Entity()
export default class Patient {
@PrimaryGeneratedColumn("uuid")
id!: string;
@MaxLength(100)
@MinLength(1, { message: "The name cannot be empty" })
@Column({
length: 100,
unique: true,
})
patientName!: string;
@CreateDateColumn()
createdAt!: Date;
}
Edit 2: I'm running the code by using the two scripts I've created
yarn watch (tsc -w)
followed by
yarn dev (nodemon dist/index.js)
Edit 3:
Here is the output of running tsc for that file in particular
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const typeorm_1 = require("typeorm");
const class_validator_1 = require("class-validator");
let Patient = class Patient {
};
__decorate([
(0, typeorm_1.PrimaryGeneratedColumn)("uuid"),
__metadata("design:type", String)
], Patient.prototype, "id", void 0);
__decorate([
(0, class_validator_1.MaxLength)(100),
(0, class_validator_1.MinLength)(1, { message: "The name cannot be empty" }),
(0, typeorm_1.Column)({
length: 100,
unique: true,
}),
__metadata("design:type", String)
], Patient.prototype, "patientName", void 0);
__decorate([
(0, typeorm_1.CreateDateColumn)(),
__metadata("design:type", Date)
], Patient.prototype, "createdAt", void 0);
Patient = __decorate([
(0, typeorm_1.Entity)()
], Patient);
exports.default = Patient;
//# sourceMappingURL=Patient.js.map
CodePudding user response:
Turns out the issue was actually on my ormconfig.json
.
The entities
and migrations
options were pointing to the original .ts
files in src/
, when (obviously, in retrospect), they should be pointing to the dist/
.js
files.
Well, those were some interesting hours on my life down the drain...