使用 github 打字稿作为依赖项 - SyntaxError:不能在模块外使用导入语句

时间:2021-06-04 15:21:15

标签: node.js typescript import package

我有 2 个不同的打字稿存储库,其中一个 (repoA) 依赖于另一个 (repoB) 作为依赖项。当我在本地编译 repoB 时,它会按预期构建而没有任何错误,但是当 repoA 尝试调用命令时:

"watch-and-run": "ts-node-dev --respawn -- src/index.ts",

出现错误:

/repoB/src/index.ts:1
import { genericScraper } from "./scrapers/GenericScraper";
SyntaxError: Cannot use import statement outside a module

被扔到 repoA 中的 repoB 的打包版本中。但是,当我为 repoA 运行构建命令时,没有错误。

这是 repoB 的 tsconfig.json 文件:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true
  },
  "include": [
    "./src"
  ] 
}

这是 repoA 的 package.json 文件:

{
  "name": "repoA",
  "version": "1.0.0",
  "main": "dist/index.js",
  "license": "MIT",
  "module": "commonjs",
  "private": true,
  "dependencies": {
    "@hapi/bell": "^12.2.0",
    "@hapi/hapi": "^20.1.2",
    "@hapi/podium": "^4.1.3",
    "@types/bcrypt": "^3.0.1",
    "@types/hapi__bell": "^11.0.2",
    "@types/jsonwebtoken": "^8.5.1",
    "@types/passport": "^1.0.6",
    "@types/passport-facebook": "^2.1.10",
    "@types/pg": "^7.14.11",
    "@types/ramda": "^0.27.40",
    "@types/uuid": "^8.3.0",
    "@types/valid-url": "^1.0.3",
    "@typescript-eslint/eslint-plugin": "^4.22.0",
    "ava": "^3.15.0",
    "axios": "^0.21.1",
    "bcrypt": "^5.0.1",
    "bluebird": "^3.7.2",
    "cheerio": "^1.0.0-rc.9",
    "crypto": "^1.0.1",
    "dotenv": "^8.2.0",
    "eslint": "^7.24.0",
    "eslint-config-airbnb": "^18.2.1",
    "express": "^4.17.1",
    "got": "^11.8.2",
    "hapi-auth-cookie": "^10.0.0",
    "hapi-auth-jwt": "^4.0.0",
    "hapi-auth-jwt2": "^10.2.0",
    "http-status-codes": "^2.1.4",
    "joi": "^17.4.0",
    "jsonwebtoken": "^8.5.1",
    "nodemon": "^2.0.7",
    "pg": "^8.6.0",
    "pg-connection-string": "^2.5.0",
    "ramda": "^0.27.1",
    "repoB": "https://github.com/username/repoB",
    "ts-node-dev": "^1.1.6",
    "tslint": "^6.1.3",
    "typescript": "^4.2.4",
    "uuid": "^8.3.2",
    "valid-url": "^1.0.9"
  },
  "scripts": {
    "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
    "upgrade-repoB": "yarn upgrade repoB && cd node_modules/recipe-scraper && yarn build",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "watch-and-run": "ts-node-dev --respawn -- src/index.ts",
    "lint": "./node_modules/.bin/eslint --fix 'src/**/*.ts'",
    "test": "yarn ava 'tests/**/*.spec.ts'"
  },
  "devDependencies": {
    "@shopify/eslint-plugin": "^40.1.0",
    "@types/hapi__hapi": "^20.0.6",
    "@typescript-eslint/parser": "^4.22.0"
  },
  "ava": {
    "extensions": [
      "ts"
    ],
    "require": [
      "ts-node/register"
    ]
  }
}

我可以看到它正在调用 src 文件夹而不是 build 文件夹,但我不确定这是否是问题所在/如何更改它以便它指向 repoB 的 build 文件夹中的 index.js。

0 个答案:

没有答案