TypeScrip和Node.js错误&Quot;语法错误:不能在模块&Quot;之外使用导入语句 [英] Typescript and Node.js error "SyntaxError: Cannot use import statement outside a module"

查看:0
本文介绍了TypeScrip和Node.js错误&Quot;语法错误:不能在模块&Quot;之外使用导入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行rpm Run Start后,打印错误:语法错误:无法在模块之外使用导入语句。 注意:我正在将其用于GCP Cloud Run。

Package.json

 {
  "name": "auth-api-public",
  "version": "0.0.1",
  "main": "src/index.ts",
  "type": "module",
  "scripts": {
    "start": "node src/index.ts",
    "lint": "eslint src/**.ts",
    "fix": "eslint --fix *.ts"
  },
  "author": "Jacob Miller",
  "engines": {
    "node": ">=12.0.0"
  },
  "dependencies": {
    "@google-cloud/secret-manager": "^3.9.0",
    "express": "^4.17.1",
    "firebase-admin": "^9.11.0",
    "stytch": "^3.0.2"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.30.0",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "8.3.0",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-promise": "5.1.0"
  }
}
然后我有一个名为src的文件夹,其中的文件包含index.ts中的导入语句(请参阅带有文件结构的屏幕截图)。

这是导入语句代码:

import * as express from "express";
import * as stytch from "stytch";
import * as admin from "firebase-admin";

这是tsconfig.json:

    {
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  }
}

    // {
    //   "compilerOptions": {
    //     "outDir": "./built",
    //     "allowJs": true,
    //     "target": "es6",
    //     "noImplicitReturns": true,
    //     "noUnusedLocals": true,
    //    "moduleResolution": "node",
    //     "sourceMap": true,
    //     "strict": false,
    //   },
    //   "include": ["src"],
    //   "compileOnSave": true,
    // }
然后运行NPM Run Start屏幕截图后显示错误消息:

推荐答案

您可以查看堆栈溢出问题12,其中OP遇到了类似的错误。 您还可以参考link1link2,其中提出了类似的问题,并讨论了语法错误:无法在模块之外使用IMPORT语句。

确保您使用的是NodeJS的最新版本,如果版本早于13,则您还必须在Package.json文件中添加标志以及上述更改。

语法:

         node --experimental-modules program.js

这篇关于TypeScrip和Node.js错误&Quot;语法错误:不能在模块&Quot;之外使用导入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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