【发布时间】:2021-10-02 17:04:00
【问题描述】:
所以,我正在尝试开始一个新项目,但我一直遇到以下错误:
(因为不知何故我还不能嵌入图像) 错误:SyntaxError: Cannot use import statement outside a module
我发现的常见解决方案,例如编辑 tsconfig.json 或在 package.json 中添加 "type":"modules" 对我没有任何帮助。
这是我的 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"
}
}
...还有我的 tsconfig.json 文件:
{
"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"]
}
我不知道该怎么做,尤其是因为我似乎无法找到如何使 TypeORM 使用“require”语法来解决问题...感谢任何帮助。
提前致谢。
编辑 1:
导致我出现问题的文件位于:src/models/User.ts,如下:
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;
}
编辑 2: 我正在使用我创建的两个脚本运行代码
纱表 (tsc -w)
紧随其后
纱线开发(nodemon dist/index.js)
编辑 3:
这里是特别为那个文件运行 tsc 的输出
"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
【问题讨论】:
-
请分享你是如何做到这一点的
-
你需要什么信息,@DanielA.White?
-
就是这样,代码库已经几乎不存在了。除了导致我出现问题的 .ts 文件(我已经添加)之外,程序唯一要做的就是在端口 3000 上创建一个服务器,实际上没有别的。我还添加了用于运行程序的命令。
-
当你有
"module": "commonjs"时,为什么你的tsc输出import语句?有些东西看起来很奇怪,你是怎么运行这个的?
标签: javascript node.js typescript express typeorm