简体   繁体   English

无法在后端使用 typescript 的模块外部使用导入语句

[英]Cannot use import statement outside a module using typescript on backend

Currently using TypeScript on a Node backend, but I keep getting the following error when I attempt to import my mongoDB model:目前在节点后端使用 TypeScript,但是当我尝试导入我的 mongoDB model 时,我不断收到以下错误:

(node:47933) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
import Users from './models/Login';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)

My package.json currently looks like this:我的 package.json 目前看起来像这样:

{
  "name": "backend",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "bcrypt": "^5.0.0",
    "cookie-session": "^1.4.0",
    "express": "^4.17.1",
    "mongoose": "^5.11.8",
    "node-ts": "^5.1.1",
    "passport": "^0.4.1",
    "passport-facebook": "^3.0.0",
    "passport-local": "^1.0.0"
  },
  "scripts": {
    "server": "nodemon server.ts"
  },
  "devDependencies": {
    "@types/bcrypt": "^3.0.0",
    "@types/express": "^4.17.9",
    "@types/mongoose": "^5.10.3",
    "@types/node": "^14.14.14",
    "@types/passport": "^1.0.4",
    "dotenv": "^8.2.0",
    "nodemon": "^2.0.6",
    "ts-node": "^9.1.1"
  }
}

and the tsconfig file looks like this: tsconfig 文件如下所示:

{
  "compilerOptions": {
    /* Basic Options */
    "module": "esnext", 
    "target": "ES2018",
    "outDir": "./dist",
    /* Strict Type-Checking Options */
    "strict": true,      
    "esModuleInterop": true,
    /* Source Map Options */
    "inlineSourceMap": true, 
    "noImplicitAny": false
  }, 
  "allowJs": true,
  "skipLibCheck": true,
  "esModuleInterop": true,
  "allowSyntheticDefaultImports": true,
  "strict": true,
  "forceConsistentCasingInFileNames": true,
  "noFallthroughCasesInSwitch": true,
  "moduleResolution": "node",
  "resolveJsonModule": true,
  "isolatedModules": true,
  "noEmit": true
}

As of now I've tried:截至目前,我已经尝试过:

  1. Adding "type":"module" to my package.json folder将“类型”:“模块”添加到我的 package.json 文件夹
  2. Changing my "module" to esnext/commonjs and/or changing my target to esnext/es6/es2020将我的“模块”更改为 esnext/commonjs 和/或将我的目标更改为 esnext/es6/es2020

In your package.json add:在您的 package.json 中添加:

"type": "module"

just below "main": "index.js",就在"main": "index.js",

try adding typescript as dev dependency尝试添加 typescript 作为开发依赖项

npm install typescript --save-dev

also change module property to commonjs in tsconfig.json还将module属性更改为commonjs中的 commonjs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest with TypeScript (Backend - Node/Express) 说 Cannot use import statement outside a module - Jest with TypeScript (Backend - Node/Express) says Cannot use import statement outside a module Typescript 与 mocha - 无法在模块外使用导入语句 - Typescript with mocha - Cannot use import statement outside a module Typescript & TypeORM:不能在模块外使用导入语句 - Typescript & TypeORM: Cannot use import statement outside a module TypeScript/Knex:不能在模块外使用 import 语句 - TypeScript/Knex: Cannot use import statement outside a module 不能在模块、Typescript、WebdriverIO 之外使用导入语句 - Cannot Use Import Statement Outside Module, Typescript, WebdriverIO Mocha + TypeScript:不能在模块外使用导入语句 - Mocha + TypeScript: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句。 我正在使用 typescript 和 discord js - SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js 使用带有 html 的脚本时,无法在模块外使用 import 语句 - Cannot use import statement outside a module, when using script with html 无法在使用 nuxtjs 的模块外部使用 import 语句 - Cannot use import statement outside a module using nuxtjs Jest 无法使用 Typescript 导入(语法错误:无法在模块外使用导入语句) - Jest fails to import with Typescript (SyntaxError: Cannot use import statement outside a module)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM