node.js - 语法错误 : Unexpected token import - import * as gulp from 'gulp'

标签 node.js typescript gulp

如果您运行 gulp,您将收到以下错误消息。

是否未安装 gulpfile 所需的模块? ..

$ gulp
[22:17:15] Failed to load external module ts-node/register
[22:17:15] Failed to load external module typescript-node/register
[22:17:15] Failed to load external module typescript-register
[22:17:15] Failed to load external module typescript-require
C:\project\interview\gulpfile.ts:1
(function (exports, require, module, __filename, __dirname) { import * as gulp f                                 rom 'gulp';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Liftoff.handleArguments (C:\Users\admin\AppData\Roaming\npm\node_modules\gu                                 lp\bin\gulp.js:116:3)

最佳答案

要使 gulp 使用 ES6 导入,您需要做 3 件事:

  1. 重命名 gulpfile.jsgulpfile.babel.js

  2. 运行这个命令:

npm i @babel/preset-env @babel/register -D
  1. 添加一个 .babelrc 文件到你的项目的根文件夹中,其中包含以下代码:
{
  "presets": [
    "@babel/preset-env"
  ]
}

您现在应该能够在 Gulp 文件中使用 ES6 导入。

注意:如果您想在 Gulp 文件中使用 TypeScript,您应该按照此处列出的步骤操作:

https://stackoverflow.com/a/49849088/1611058

关于node.js - 语法错误 : Unexpected token import - import * as gulp from 'gulp' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681396/

相关文章:

javascript - 无法在 Node.js 上使用 sqlite3 将行放入数组中

node.js - SASS;如果元素大小高于 16px,则更改 font-family

javascript - clearInterval 不停止间隔

Angular2动态组件

angular - mat-autocomplete 过滤器以突出显示部分字符串匹配

css - Gulp + Sass + Git 工作流程

javascript - 断言参数

javascript - ES 模块 : import named exports as module?

javascript - 在 gulp 中注入(inject) js 和 css 时,如何提供对 index.html 的写权限?

css - 如何解决这个 Sass 编译错误?