不能在模块外部使用 Import 语句

问题描述 投票:0回答:1

我正在尝试运行我的内置生产版本代码。使用了 Node 20.10、typescript 5 并且无法运行该构建版本。

下面是我的package.json,tsconfig.json文件。

{
  "name": "node-exporess-api",
  "version": "1.0.0",
  "description": "project",
  "author": "John",
  "engines": {
    "node": "^20.10.0"
  },
  "main": "dist/index.js",
  "scripts": {
    "start": "node ./dist/index.js",
    "dev": "nodemon",
    "build": "rm -f -r dist && tsc && tsc-alias",
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ./src/entities/index.ts",
  },
  "keywords": [],
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "mysql2": "^3.6.5",
    "reflect-metadata": "^0.2.1",
    "typeorm": "^0.3.19"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.2",
    "@types/cors": "^2.8.17",
    "@types/express": "^4.17.21",
    "@types/morgan": "^1.9.9",
    "@types/node": "^20.10.7",
    "@types/uuid": "^8.3.4",
    "@types/validator": "^13.7.5",
    "nodemon": "^3.0.2",
    "ts-node": "^10.9.2",
    "tsc-alias": "^1.8.8",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.3"
  }
}
{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "./dist/",
    "esModuleInterop": true,
    "target": "es6",
    "module": "commonjs",
    "allowJs": true,
    "moduleResolution": "node",
    "importHelpers": false,
    "preserveConstEnums": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "paths": {
      "@config/*": ["src/config/*"],
      "@config": ["src/config"],

      "@controllers/*": ["src/controllers/*"],
      "@controllers": ["src/controllers"],

      "@entities/*": ["src/entities/*"],
      "@entities": ["src/entities"],

      "@repositories/*": ["src/repositories/*"],
      "@repositories": ["src/repositories"]
  },
  "include": ["./src/"],
  "exclude": ["**/node_modules"]
}

在我的代码中,我使用如下所示的导入/导出语法:

import { MainApplication } from './app';
import os from 'os';
import express, { Application, NextFunction, Request, Response } from 'express';
npm run buld => > rm -f -r dist && tsc && tsc-alias (then finished)
npm start => 

src/migrations/6204401116637-InitDatabase.ts:1
import { MigrationInterface, QueryRunner } from 'typeorm';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at tryToRequire

我尝试将

"type": "module"
添加到 package.json 文件中。

> node ./dist/index.js

file:///User/project/dist/index.js:5
Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/User/project/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///User/project/dist/index.js:5:23
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.10.0

请帮忙。谢谢你。

node.js typescript commonjs
1个回答
0
投票

如果未安装定义导出的 CommonJS,则必须从 tsconfig.json 中删除以下行:

"module": "commonjs"

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.