我正在尝试制作同构JavaScript应用程序。
服务器端代码和客户端代码都是由Webpack编译的。
当我尝试运行包含typeorm连接的已编译服务器端捆绑包时,出现了这样的错误
错误
(node:324) UnhandledPromiseRejectionWarning: /var/www/server/database/entity/WasteGroup.ts:1
(function (exports, require, module, __filename, __dirname) { import {Entity,PrimaryGeneratedColumn, OneToMany} from "typeorm";
^
SyntaxError: Unexpected token {
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298: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)
预编译的Server.ts
// Set up ======================================================================
// get all the tools we need
import express from 'express';
import http from 'http';
import logger from 'morgan';
import path from 'path';
import {createConnection} from "typeorm";
import apiVersion1 from './api/api1';
import renderRouterMiddleware from '../iso-middleware/renderRoute';
const init = () => {
createConnection().then(() => {
require('dotenv').config();
// Configuration ===============================================================
const app = express();
app.set('port', process.env.PORT || 3000);
app.use(logger('short'));
// Request Handlers
const buildPath = path.join(__dirname, '../', 'build');
app.use('/', express.static(buildPath));
app.use('/api', apiVersion1);
app.get('*', renderRouterMiddleware);
// launch ======================================================================
// Starts the Express server on port 3001 and logs that it has started
http.createServer(app).listen(app.get('port'), () => {
console.log(`Express server started at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
}, );
})
}
export {
init
}
与Webpack相关的配置
通用
const path = require('path')
const webpack = require('webpack')
module.exports = {
mode: 'development',
devtool: '#source-map',
output: {
path: path.resolve(__dirname, '..','build'),
publicPath: '/',
filename: '[name].js'
},
module: {
rules: [
{
test: /\.(jsx?|tsx?)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', "@babel/preset-react"],
}
}
},
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
exclude: /node_modules/
},
{
enforce: "pre",
test: /\.js$/,
exclude: /node_modules/,
loader: "eslint-loader",
options: {
emitWarning: true,
failOnError: false,
failOnWarning: false,
fix:true
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ['file-loader']
}
]
},
resolve: {
alias: {
Shared: path.resolve(__dirname, '../shared')
},
extensions: ['.js','.ts', '.tsx', '.jsx', '.css', '.es6'],
},
plugins: [
// new webpack.NoEmitOnErrorsPlugin()
],
}
服务器
const path = require('path')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')
const merge = require('webpack-merge')
const commonDevConfig = require('./webpack.dev.common.js')
const server = {
target: 'node',
node: {
// Need this when working with express, otherwise the build fails
__dirname: false, // if you don't put this is, __dirname
__filename: false, // and __filename return blank or /
},
externals: [nodeExternals()],
entry: {
server: './server/server.ts',
},
output:{
libraryTarget:'commonjs2'
}
}
module.exports = merge(commonDevConfig, server)
WasteGroup.ts
import {Entity,PrimaryGeneratedColumn, OneToMany} from "typeorm";
import {WasteType} from './WasteType'
@Entity()
export class WasteGroup {
@PrimaryGeneratedColumn()
id: number;
@OneToMany(type => WasteType, WasteType => WasteType.wasteGroup)
types:WasteType;
}
ormconfig.json
{
"type": "mysql",
"host": "mysql",
"port": 3306,
"username": "nodedock",
"password": "nodedock",
"database": "wietlin_osada_db",
"entities": ["server/database/entity/*.ts"],
"migrations":[
"server/database/migration/*.ts"
],
"cli":{
"migrationsDir":"server/database/migration",
"entitiesDir": "server/database/entity"
},
"logging": true,
"synchronize": false
}
tsconfig.json
{
"compilerOptions": {
"module":"es6",
"target":"es6",
"moduleResolution":"node",
"noImplicitAny": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports":true,
"removeComments": true,
"preserveConstEnums": true,
"experimentalDecorators":true,
"emitDecoratorMetadata":true,
// "noEmit":true,
"jsx":"react",
"sourceMap":true
},
"include": [
"server/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
使用此类npm脚本在Webpack编译后运行应用程序
"runNodeServerApp":"node -e 'require(\"./build/server.js\").init()'"
当我在运行没有TypeORM连接的应用程序时,它确实可以正常工作。
是TypeORM还是我的设置出现了这个问题?
这是我的项目Tree,如果它可以帮助任何人。
├── build
│ ├── main.js
│ ├── main.js.map
│ ├── server.js
│ └── server.js.map
├── client
│ └── main.js
├── configs
│ ├── webpack.dev.client.js
│ ├── webpack.dev.common.js
│ └── webpack.dev.server.js
├── iso-middleware
│ └── renderRoute.js
├── ormconfig.json
├── package.json
├── package-lock.json
├── README.md
├── server
│ ├── api
│ │ └── api1.ts
│ ├── controller
│ │ └── WastePickupController.ts
│ ├── database
│ │ ├── entity
│ │ │ ├── WasteGroup.ts
│ │ │ ├── WastePickupEvent.ts
│ │ │ └── WasteType.ts
│ │ ├── migration
│ │ │ └── 1567256869205-Init.ts
│ │ └── repositories
│ │ └── TestRepository
│ │ └── index.js
│ └── server.ts
├── shared
│ ├── App.js
│ ├── components
│ │ ├── HTML.js
│ │ ├── icons
│ │ │ └── DumpTruck
│ │ │ └── index.js
│ │ ├── Main.js
│ │ ├── NotFound.js
│ │ ├── pages
│ │ │ └── Home
│ │ │ ├── components
│ │ │ │ ├── MainAppBar
│ │ │ │ │ └── index.js
│ │ │ │ ├── WasteScheduleCard
│ │ │ │ │ ├── DumpTruck
│ │ │ │ │ │ └── index.js
│ │ │ │ │ ├── index.js
│ │ │ │ │ └── WasteScheduleLegendOpener
│ │ │ │ │ └── index.js
│ │ │ │ ├── WeatherCard
│ │ │ │ │ └── index.js
│ │ │ │ └── WeatherWidget
│ │ │ │ └── index.js
│ │ │ └── index.js
│ │ └── Root.js
│ ├── renderFullApp.js
│ ├── routes.js
│ └── theme
│ └── index.js
└── tsconfig.json
答案 0 :(得分:1)
这里的问题是,您在ormconfig.json中引用TypeScript文件,并且节点服务器无法处理TypeScript。您需要转译它们并引用JavaScript文件。 另请参见此post。