打字稿/节点意外令牌 *

jaj*_*arr 10 commonjs node.js typescript webpack

我正在尝试将我的应用程序节点后端更新为打字稿,但我一直遇到此语法错误:

/Users/Shared/website/src/server.ts:1
(function (exports, require, module, __filename, __dirname) { import * 
as express from 'express';
                                                                 ^

SyntaxError: Unexpected token *
Run Code Online (Sandbox Code Playgroud)

我的 tsconfig/webpack/server 等设置如下:

服务器.ts

import * as express from 'express';
import * as path from 'path';

const app = express();
const port = process.env.PORT || 3000;

app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', function(req, res, next){
    res.sendFile(path.resolve(__dirname, '/public', 'index.html'));
});

app.listen(port, () => console.log(`Listening on port ${port}!`));
Run Code Online (Sandbox Code Playgroud)

webpack.config.json

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const outputDirectory = 'dist';

module.exports = {
    entry: "./src/index.tsx",
    output: {
        filename: "bundle.js",
        path: path.join(__dirname, outputDirectory)
    },
    mode: 'development',

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            { test: /\.tsx?$/, loader: "ts-loader" },

            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" },

            {
                test: /\.scss$/,
                use: [
                    "style-loader", // creates style nodes from JS strings
                    "css-loader", // translates CSS into CommonJS
                    "sass-loader" // compiles Sass to CSS, using Node Sass by default
                ]
            }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
    },

    plugins: [
        new CleanWebpackPlugin([outputDirectory]),
        new HtmlWebpackPlugin({
          template: './public/index.html',
          favicon: './public/favicon.gif'
        }),
        new CopyWebpackPlugin([
            { from: 'public' }
        ])
    ]
};
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": false,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react"
    },
    "include": [
        "./src/**/*"
    ],
}
Run Code Online (Sandbox Code Playgroud)

构建过程成功,但我在运行时遇到了语法错误。我有一个使用 typescript / tsx 设置的 React 前端,它工作得很好。我似乎只是遇到了 server.ts / node 文件的问题。我对尝试设置这一切很陌生,但我想练习使用多种技术(react/node/typescript/sass/webpack/etc)制作网站。到目前为止,除了打字稿/节点关系之外,我拥有一切。

fre*_*dev 6

在我意识到未应用 tsconfig 之后,我遇到了同样的问题。

如果

"module": "commonjs" 
Run Code Online (Sandbox Code Playgroud)

实际上,你不会有这个错误。


小智 5

我之前也遇到过同样的问题。我通过更改导入“调用”来解决它:

从:

import * as express from 'express';
Run Code Online (Sandbox Code Playgroud)

到:

const express = require('express');
Run Code Online (Sandbox Code Playgroud)

  • 为了澄清为什么会这样,Node 实际上还不支持 ES6 模块。查看我能够挖掘的这些链接:[实验支持](https://nodejs.org/api/esm.html),[一个相关的 SO 问题](/sf/ask/3209791861/ /how-can-i-use-an-es6-import-in-node) 和 [GitHub 问题](https://github.com/nodejs/modules/issues/253) (2认同)