node.js:意外的令牌{ [英] node.js: Unexpected token {

查看:30
本文介绍了node.js:意外的令牌{的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 TypeScript 为 mocha 编写了一个单元测试.我正在尝试使用 gulp 运行它(这在这里并没有真正发挥作用).我收到以下异常:

So I have a unit test written for mocha using TypeScript. I am trying to run it using gulp (which doesn't really play a part here). I get the following exception:

(function (exports, require, module, __filename, __dirname) { import { assert } from 'chai';
                                                                     ^

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)

有人能告诉我在我的 tsconfig.json 中需要什么设置来解决这些问题吗?

Could someone tell me what setting in need in my tsconfig.json to fix problems like these?

node -v 
v10.6.0
tsc -v
Version 2.9.2

这是我的 tsconfig.json:

and here's my tsconfig.json:

{
    "include" : [
        "src",
        "test",
        "unittest"
    ],
    "compileOnSave": true,
    "compilerOptions": {
        "module": "es2015",
        "moduleResolution": "node",
        "esModuleInterop": true,
        "target": "es5",
        "noImplicitAny": true,
        "declaration": true,
        "sourceMap": true,
        "preserveConstEnums": true,
        "lib": [
            "es2015", "dom"
        ],
        "noUnusedLocals": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "strictNullChecks": false,
        "noUnusedParameters": false,
        "pretty": true,
        "allowUnreachableCode": false,
        "experimentalDecorators": true,
        "suppressImplicitAnyIndexErrors": true,
        "outDir": "./build"
    }
}

推荐答案

Node 还不完全支持 import方式.

Node doesn't fully support import yet or at least not by default, so errors will happen when importing using the import in that way.

当使用 TypeScript 时,你应该在 compilerOptions 中使用 "module": "commonjs",因为这是 node.js 使用的.编译时,TypeScript 会将所有 imports 转换为节点支持的 require.

When using TypeScript you should use "module": "commonjs" in your compilerOptions, because that is what node.js uses. When compiled, TypeScript will convert all the imports to node supported require's.

这篇关于node.js:意外的令牌{的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆