【问题标题】:How to use a git+https dependency correctly如何正确使用 git+https 依赖
【发布时间】:2021-08-03 16:06:49
【问题描述】:

我正在尝试使用 git+https 依赖项(在 github 上)来制作打字稿库。作为示例,我将其缩减为单个文件,但它仍然失败。使用文件依赖关系完美。切换到 git+https 依赖会导致我收到错误:

export const Greeter = (name: string) => `Hello ${name}`; 
^^^^^^

SyntaxError: Unexpected token 'export'

这两个依赖项,两个项目都没有其他变化:

"@dgmyers/greeter": "git+https://git@github.com:dgmyers/typescript-greeter-library.git#v0.1.1",
"@dgmyers/greeter": "file:../typescript-greeter-library",

文件: typescript-greeter-library/src/hello.ts

export const Greeter = (name: string) => `Hello ${name}`;

typescript-greeter-library/dist/hello.d.ts(tsc 生成)

export declare const Greeter: (name: string) => string;

typescript-greeter-library/package.json

{
  "name": "typescript-greeter-library",
  "version": "1.0.0",
  "description": "",
  "main": "src/hello.ts",
  "types": "dist/hello.d.ts",
  "scripts": {
    "clean": "rm -rf dist/*",
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/dgmyers/typescript-greeter-library.git"
  },
  "author": "",
  "license": "UNLICENSED",
  "bugs": {
    "url": "https://github.com/dgmyers/typescript-greeter-library/issues"
  },
  "homepage": "https://github.com/dgmyers/typescript-greeter-library#readme"
}

typescript-greeter-library/tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "lib": ["es2020"],
    "target": "es5",
    "declaration": true,
    "moduleResolution": "node",
    "emitDeclarationOnly": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "baseUrl": "./node_modules",
    "outDir": "./dist",
    "module": "commonjs",
    "typeRoots": [
      "./types"
    ]
  }
}

消费者/package.json

{
  "name": "consumer",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "run": "ts-node src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "UNLICENCED",
  "dependencies": {
    "@dgmyers/greeter": "git+https://git@github.com:dgmyers/typescript-greeter-library.git#v0.1.2",
    "@types/node": "^16.3.1",
    "ts-node": "^10.1.0",
    "typescript": "^4.3.5"
  }
}

消费者/index.ts

import { Greeter } from '@dgmyers/greeter'
console.log(`${Greeter('dgmyers')}`);

【问题讨论】:

  • 您的typescript-greeter-library/package.json 文件中需要"type": "module"
  • 嗨@Aurast。你的建议对我不起作用,我得到同样的错误。我试图了解文件依赖项和 git 依赖项之间的区别。不过,我将进一步研究 type: module 建议。

标签: node.js typescript npm


【解决方案1】:

通过源代码库安装时,即。通过github:git+https: 协议,npm 将运行prepare 生命周期脚本并像发布它一样打包它。

通常,即使您的源代码库不是纯 js(例如用 typescript 编写),它仍然可以,因为用于编译源代码的脚本(例如 tsc)通常在 prepare 中配置,并且 npm 会在您安装时编译它回购。

但是,仍有 2 种情况会失败。

  1. prepare 中缺少编译脚本
  2. 在源代码库中缺少 .npmignore 文件,因此 npm 回退到 .gitignore,这通常不包括包含已编译代码的文件夹

也许你是其中之一。

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2021-11-16
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多