NPM Package如何导出文件以在其他项目中使用?

时间:2019-05-25 21:10:59

标签: typescript npm

我正在尝试创建一个简单的npm包,以便在项目之间共享一些打字稿模型。

在我的lib文件夹中,有以下文件:

lib / model1.ts

export default class Model1 {
    name: string;
}

lib / model2.ts

export default class Model2 {
    count: number;
}

lib / index.ts

import Model1 from "./model1";
import Model2 from "./model2";

export = {Model1, Model2};

我需要像这样导出我的模型吗?

我的package.json如下:

{
  "name": "project-name",
  "version": "1.0.0",
  "main": "lib/index.ts",
  "scripts": {
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "license": "ISC",
  "devDependencies": {
    "typescript": "^3.4.5"
  }
}

但是当我尝试在另一个项目中使用这些模型时,会出现以下错误:

ERROR in /Users/jonas/Projects/ProjectFit/models/lib/model1.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: /Users/jonas/Projects/ProjectFit/models/lib/model1.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at AngularCompilerPlugin.getCompiledFile (/Users/jonas/Projects/ProjectFit/cms/angular/project-fit-cms/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:719:23)
    at plugin.done.then (/Users/jonas/Projects/ProjectFit/cms/angular/project-fit-cms/node_modules/@ngtools/webpack/src/loader.js:41:31)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
ERROR in /Users/jonas/Projects/ProjectFit/models/lib/model2.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: /Users/jonas/Projects/ProjectFit/models/lib/model2.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at AngularCompilerPlugin.getCompiledFile (/Users/jonas/Projects/ProjectFit/cms/angular/project-fit-cms/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:719:23)
    at plugin.done.then (/Users/jonas/Projects/ProjectFit/cms/angular/project-fit-cms/node_modules/@ngtools/webpack/src/loader.js:41:31)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

0 个答案:

没有答案