我正在使用“ ts-node”运行的Typescript项目。
$ ts-node .\src\index.ts
it works =)
但是我想将其编译为Javascript。所以我跑了。
$ tsc
$ node .\src\index.js
但是我得到了休闲的错误:
(node:4392) UnhandledPromiseRejectionWarning: \event-monitor\src\models\Alarm.ts:1
(function (exports, require, module, __filename, __dirname) { import BaseEntity from "./BaseEntity";
^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
(node:4392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:4392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我不理解为什么使用ts-node可以正常运行,但是使用TSC却无法正常工作。
这是我的ts-config文件
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"strictNullChecks": false,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"sourceMap": true,
"outDir": "dist",
"noImplicitAny": false,
"strictPropertyInitialization": false,
"emitDecoratorMetadata": true,
"types": ["mocha", "chai", "node"],
"paths": { "@/*": ["src/*"] },
"lib": ["es5", "es6"]
},
"include": ["src/**/*.ts", "tests/**/*.ts", "repoTest.ts"],
"exclude": ["node_modules"]
}
我正在使用的模块:
"lodash": "^4.17.11",
"mysql": "^2.17.1",
"reflect-metadata": "^0.1.13",
"restify": "^8.3.0",
"sqlite3": "^4.0.6",
"typeorm": "^0.2.16",
"typescript": "^3.0.3"
答案 0 :(得分:0)
Node不支持ES6模块,因此您需要使用CommonJS模块的特殊TyeScript格式导入和导出模块,例如
export = BaseEntity;
和
import BaseEntity = require('./BaseEntity');
中对此进行了记录(不是很好)。
答案 1 :(得分:0)
我只是想知道会发生什么。
我使用的是TypeORM,它正在为其实体寻找TS文件。
但是在“ js模式”下,我必须配置TypeOMR来代替dist / js文件。