【发布时间】: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