语法错误:使用我在TypeScript中创建的NPM包时出现意外标记“export”

2uluyalo  于 2023-05-29  发布在  TypeScript
关注(0)|答案(1)|浏览(122)

我在TypeScript中创建了自己的npm包。我从另一个项目导入包,如下所示:

import {computeSum} from '@charneykaye/compute-sum';

但这会导致一个错误:

Details:

    /home/runner/work/consume-sum/consume-sum/packages/example/node_modules/@charneykaye/compute-sum/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './feature';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import {computeSum} from '@charneykaye/compute-sum';
        | ^
      2 |
      3 | export const computeTripleSum = (a: number, b: number, c: number) => {
      4 |   return computeSum(computeSum(a, b),c)

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)
      at Object.require (src/implementation.ts:1:1)
      at Object.<anonymous> (src/__tests__/feature-test.ts:2:1)

以下是发布包的示例存储库中的所有代码:

package.json
{
  "name": "@charneykaye/compute-sum",
  "version": "1.0.4",
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/charneykaye/compute-sum.git"
  },
  "description": "Typescript package for testing",
  "author": "Charney Kaye <hi@charneykaye.com>",
  "license": "MIT",
  "files": [
    "*.js",
    "*.ts"
  ],
  "pika": true,
  "sideEffects": false,
  "scripts": {
    "start": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
    "start:windows": "nodemon --watch 'src/**/*.ts' --exec \\\"npx ts-node\\\" src/index.ts",
    "build": "npx tsc",
    "create": "npm run build && npm run test",
    "catalog": "npx ts-node src/index.ts",
    "test": "tsc -p . && jest --coverage --verbose --runInBand"
  },
  "dependencies": {
    "commander": "^10.0.0",
    "dotenv": "^16.0.3",
    "figlet": "^1.5.2",
    "octokit": "^1.8.0",
    "stemmer": "^1.0.5",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.5"
  },
  "devDependencies": {
    "@jest/globals": "^29.4.1",
    "@types/jest": "^29.4.0",
    "@types/node": "^18.15.3",
    "jest": "^29.4.1",
    "nodemon": "^2.0.20",
    "ts-jest": "^29.0.5"
  },
  "publishConfig": {
    "access": "restricted"
  },
  "bugs": {
    "url": "https://github.com/charneykaye/compute-sum/issues"
  },
  "homepage": "https://github.com/charneykaye/compute-sum#readme",
  "main": "dist/index.js",
  "types": "dist/index.d.ts"
}
tsconfig.json
{
  "compilerOptions": {
    "declaration": true,
    "esModuleInterop": true,
    "moduleResolution": "Node",
    "outDir": "dist",
    "resolveJsonModule": true,
    "rootDir": "src",
    "strict": true,
    "target": "es6",
    "types": [
      "node",
      "jest"
    ]
  }
}
src/index.ts
export * from './feature'
src/feature.ts
export const computeSum =(a: number, b: number): number => a + b
.github/workflows/tag.yml
name: "Release (Tag)"

on:
  push:
    tags:
      - '*'

jobs:
  CI:
    name: Build, Test, Publish
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:

      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18.14
          cache: 'npm'
          registry-url: 'https://npm.pkg.github.com/'
          scope: '@charneykaye'

      - name: Install npm packages from lockfile
        run: npm ci

      - name: Unit tests
        run: npm test

      - name: Build distribution
        run: npm run build

      - name: Finalize distribution files
        run: |
          cp .npmrc dist/
          cp README.md dist/
          cat package.json | jq 'del(.scripts)' > dist/package.json
          rm -fr dist/__tests__

      - name: Publish NPM package
        working-directory: ./dist
        run: npm publish
        env:
          NPM_CONFIG_TOKEN: ${{ secrets.GITHUB_TOKEN }}
输出包结构
@charneykaye
└── compute-sum
    ├── feature.d.ts
    ├── feature.js
    ├── index.d.ts
    ├── index.js
    ├── package.json
    └── README.md
完整示例

示例项目@charneykaye/consume-sum导入示例包@charneykaye/compute-sum

q3aa0525

q3aa05251#

看起来我做了一些软件包创建到npm发布它。最值得注意的是,指定目标lib/并允许npm publish这些修改后的配置创建了一个合适的可用npm包:

package.json
{
  ...
  "files": [
    "lib/**/*"
  ],
  ...
  "scripts": {
    "build": "tsc",
    "test": "jest"
  },
  ...
  "main": "lib/index.js",
  "types": "lib/index.d.ts"
}
tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": [],
    "declaration": true,
    "outDir": "./lib",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "types": [
      "node",
      "jest"
    ]
  }
}
.github/workflows/tag.yml
- name: Publish NPM package
        run: npm publish
        env:
          NPM_CONFIG_TOKEN: ${{ secrets.GITHUB_TOKEN }}

相关问题