[英]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'
我有几个原因:
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
安装它:
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",
...
npm pack
and install the generated package in your applicationnpm 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:您的选择是:
npm link
npm link
发布到本地系统 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.