打字稿意外导入令牌

时间:2018-09-10 19:56:38

标签: typescript webpack

在node.js基本代码中使用简单的import时出现以下错误。 我想知道我的配置文件中有什么问题以及如何解决:

我收到的错误:

(function (exports, require, module, __filename, __dirname) { import { IO } from "fp-ts/lib/IO";
                                                              ^^^^^^

SyntaxError: Unexpected token import

我正在使用打字稿,这里是我的配置文件:

tsconfig.json

{
    "compilerOptions": {
        "target": "es2017",
        "module": "commonjs",
        "sourceMap": true,
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "moduleResolution": "node",
        "baseUrl": "."
    }
}

webpack.config.js

const path = require('path');

module.exports = {
  entry: 'index.ts',
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [ '.ts' ]
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

package.json

"devDependencies": {
    "@types/node": "^10.9.4",
    "jest": "^23.5.0",
    "prettier": "^1.14.2",
    "ts-loader": "^5.1.0",
    "webpack": "^4.18.0"
  }

...

  "dependencies": {
    "fp-ts": "^1.8.0",
    "typescript": "^3.0.3"
  },

1 个答案:

答案 0 :(得分:0)

在tsconfig中,您似乎在不支持es2017样式模块的环境中运行,因此"target": "es2017",更改为"target": "es5",