#node.js #npm #node-modules #es6-modules
Вопрос:
Я хотел бы перенести часть своего кода из приложения, над которым я работаю, в отдельный пакет, где у меня могут быть константы, промежуточное программное обеспечение и т. Д. Мне удалось успешно создать пакет npm и перенести его в реестр npm. А вот структура файлов пакета.
.
├── package-lock.json
├── package.json
├── src
│ ├── constants
│ │ └── resources
│ │ ├── index.ts
│ │ └── users.ts
│ ├── errors
│ │ ├── not-found-error.ts
│ ├── index.ts
│ └── middleware
│ ├── error-handler.ts
└── tsconfig.json
Проблема, с которой я сталкиваюсь, заключается в том, что мне нужно экспортировать все мои файлы в index.ts
:
// Errors
export * from "./errors/not-found-error";
// Middleware
export * from "./middleware/error-handler";
Вот как я импортирую свои зависимости:
import { NotFoundError, errorHandler } from "@some/common";
Вместо этого я хочу иметь возможность импортировать/получать доступ к определенному подмодулю. Что-то вроде этого:
import { NotFoundError } from "@some/common/errors";
import { errorHandler } from "@some/common/middleware";
Любые предложения приветствуются!
Добавление tsconfig.json
вместе с package.json
:
// tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./build",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
// package.json
{
"name": "@some-org/common",
"version": "1.0.6",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"build/**/*"
],
"scripts": {
"clean": "del ./build/*",
"build": "npm run clean amp;amp; tsc",
"publish:patch": "npm version patch amp;amp; npm run build amp;amp; npm publish --access public"
},
"keywords": [],
"author": "myself",
"license": "ISC",
"devDependencies": {
"del-cli": "^3.0.1",
"typescript": "^4.2.4"
},
"dependencies": {}
}