简体   繁体   English

如何在 TypeScript 中创建本地模块

[英]How to create a local module in TypeScript

I have created in folder src/modules/my-module/ which has package.json and defined the main file which exports everything we need.我在文件夹src/modules/my-module/中创建了package.json并定义了导出我们需要的一切的主文件。

I can import from it now import {A} from '../../modules/my-module'我现在可以从它import {A} from '../../modules/my-module'

I want to change the syntax into import {A} from 'my-module' and I have a few reasons for it:我想将语法更改为import {A} from 'my-module'我有几个原因:

  • When I move the module to another folder, I do not want to change all the code calling this module.当我将模块移动到另一个文件夹时,我不想更改调用该模块的所有代码。
  • Later, I would like to have the possibility to move the module to a separate repository as the npm package and reuse it in multiple projects.后来,我希望有可能将该模块作为 npm 包移动到一个单独的存储库,并在多个项目中重用它。 I do not want to change all calling code later.我不想稍后更改所有调用代码。

I have managed to compile it by adding to tsconfig.json我已经设法通过添加到tsconfig.json来编译它

"paths": {
  "my-module": ["src/modules/my-module"]
}

But I can't run the result via node.js as the node can't find the module.但是我无法通过 node.js 运行结果,因为节点找不到模块。 Is there any way to use non-realtive module reference in such scenario.在这种情况下有没有办法使用非现实模块引用。

TS doesn't convert that "my-module" when transpiling your ts files to js.将 ts 文件转译为 js 时,TS 不会转换该“我的模块”。

Using module-alias package might solve your problem.使用module-alias包可能会解决您的问题。

Add this configuration below into package.json:将下面的配置添加到 package.json 中:

"_moduleAliases": {
   "my-module": "<your_build_folder>/modules/my-module"
},

And this code on first line of your main file (server.ts/index.ts)这段代码位于主文件的第一行 (server.ts/index.ts)

import 'module-alias/register';

By the sounds of it, what you're wanting to do is package up your local my-module so that it can be used in the same way you'd install and use a package from the npm registry .听起来,你想要做的是打包你的本地my-module ,这样它就可以像你安装和使用来自 npm registry 的包一样使用

When you're doing local development, its easy to configure a dependency to reference to your module as a file path - though you need to have your typescript transpiled for it to work in your case.当您进行本地开发时,很容易配置一个依赖项以将您的模块作为文件路径引用——尽管您需要转换您的打字稿才能使其在您的情况下工作。

Here's the method I'm using for local development, in an environment where we have many utility modules for a microservices architecture.这是我在本地开发中使用的方法,在我们有许多微服务架构实用程序模块的环境中。 I package the module into an archive and install it using npm install :我将模块打包到存档中并使用npm install安装它:


  • Use npm pack to package the module into a .tgz .使用npm pack将模块打包成.tgz Our package.json defines the target directory to be packaged, and the build script performs the transpile into the target (obviously adjust for your needs):我们的package.json定义了要打包的目标目录, build脚本执行到目标的转译(显然根据您的需要进行调整):
  ...
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": [
    "dist"
  ],
  "scripts": {
    "build": "npx babel src --out-dir dist --extensions .ts,.tsx,.js --ignore **/*.test.ts,**/*.test.tsx",
  ...
  • Run npm pack and install the generated package in your application运行npm pack并在您的应用程序中安装生成的包
/my-module/> npm pack
/my-module/> cd ../my-app
/my-app/> npm install --save ../my-module/my-module-0.0.1.tgz

Or as an all-in-one (builds tgz in my-app dir):或者作为一个一体机(在my-app目录中构建tgz ):

/my-app/> && npm pack ../my-module && npm i -s my-module-0.0.1.tgz

Once you're done with development, you'll probably want to publish your module in a way that its available to your project(s) on deployment.完成开发后,您可能希望以可用于部署项目的方式发布模块。

Your options are along the lines of:您的选择是:

  • Publish to your local system using npm link使用npm link发布到本地系统
  • Publish to a private registry发布到私有注册表
  • Publish to the npm registry (as either a public or private module)发布到 npm 注册表(作为公共或私有模块)

Here's a good resource for these options: https://medium.com/@debshish.pal/publish-a-npm-package-locally-for-testing-9a00015eb9fd这是这些选项的一个很好的资源: https ://medium.com/@debshish.pal/publish-a-npm-package-locally-for-testing-9a00015eb9fd

You can transpile your external local project (reference project) since Typescript 3 in July 2018.从 2018 年 7 月的 Typescript 3 开始,您可以转译您的外部本地项目(参考项目)。

See: How to share code between TypeScript projects?请参阅: 如何在 TypeScript 项目之间共享代码?

Add local module as dependency to package.json (in the root of your project):将本地模块作为依赖项添加到package.json (在项目的根目录中):

  "dependencies": {
    "my-module": "file:src/modules/my-module",
    ...
  }

Configure your typescript settings like here @tsconfig/recommended像这里@tsconfig/recommended配置你的打字稿设置

Run npm install my-module in your root folder在您的根文件夹中运行npm install my-module

Then you can do:然后你可以这样做:

import {A} from 'my-module'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM