【发布时间】:2020-02-08 00:07:19
【问题描述】:
我刚刚从 react-express-starter 创建了一个应用程序。 在里面我有一个“服务器”文件夹,nodejs 使用它来创建后端。在这个文件夹中,我有一个 index.js 文件:
const express = require('express');
const app = express();
const { Test } = require('./test/index.ts');
app.listen(3001, () => {
console.log('Express server is running on localhost:3001');
Test.run();
});
我需要从一个ts文件中调用一个方法,
服务器/测试/test.ts:
export class Test {
static run(){
console.log('OK THIS WORKS');
}
}
服务器/测试/index.ts
export { Test as test} from './test';
运行服务器:
export { Test as test } from './test';
^^^^^^
SyntaxError: Unexpected token export
再试一次,将这个添加到 index.js 的第一行:
import { Test as test } from './test';
结果:
import { Test as test } from './test';
^
SyntaxError: Unexpected token {
现在更改 index.js 中的第一行:
import * as test from './test';
结果:
import * as test from './test';
^
SyntaxError: Unexpected token *
我如何让事情发挥作用?
【问题讨论】:
-
import { Test as test } from './test'; -
您好,Ihor,我已将您建议的内容添加到 index.js 文件中,结果为:C:\Source\react-express-starter-master\server\index.js:2从'./test'导入{测试作为测试}; ^ SyntaxError: Unexpected token {
标签: javascript node.js reactjs typescript