简体   繁体   English

如何将 Node.js 的 TypeScript 代码编译为一个文件?

[英]How do I compile my TypeScript code for Node.js to one file?

I want to compile TypeScript to one file when using with Node.js.我想在与 Node.js 一起使用时将 TypeScript 编译为一个文件。

I have tried configuring "tsconfig.json" like this:我试过像这样配置“tsconfig.json”:

"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": false,
    "sourceMap": false,
    "outFile": "./app/index.js",
    "pretty": true,
    "types": [
      "node"
    ]
  }",

...but when I try with module" set to "commonjs"`, I get the error: ...但是当我尝试将module" set to "commonjs"` 时,出现错误:

error TS6082: Only 'amd' and 'system' modules are supported alongside --outFile.

If I change it to "module": "system" , I get this error when running the file in node :如果我将其更改为"module": "system" ,则在node运行文件时会出现此错误:

ReferenceError: System is not defined

If I change module to "amd" , I get this error when running the file in node :如果我将module更改为"amd" ,则在node运行文件时会出现此错误:

ReferenceError: define is not defined

It is not possible to bundle Node.js modules into a single file with the TypeScript compiler alone: each file in the CommonJS module system (used by Node.js) is treated as a single module, and cannot be joined together without proper module bundling techniques found in the many JavaScript code bundlers out there (such as Browserify, Webpack, Rollup, Parceljs, ...).单独使用 TypeScript 编译器无法将 Node.js 模块捆绑到单个文件中:CommonJS 模块系统(由 Node.js 使用)中的每个文件都被视为单个模块,如果没有适当的模块捆绑,则无法连接在一起在许多 JavaScript 代码打包器(例如 Browserify、Webpack、Rollup、Parceljs 等)中发现的技术。

Other module systems such as SystemJS do not have this limitation, and module definitions can be concatenated into a single file using just the TypeScript compiler: tsc ... --outfile bundle.js -module system .其他模块系统如 SystemJS 没有这个限制,模块定义可以只使用 TypeScript 编译器连接成一个文件: tsc ... --outfile bundle.js -module system However, they are not supported by Node.js on the fly.但是,它们不受 Node.js 动态支持。

For the actual solution, there are two reasonable choices: either configure your project to bundle the solution using a separate tool (Browserify, Webpack, ...), or include an implementation of SystemJS into your Node.js application (instructions here ).对于实际的解决方案,有两个合理的选择:要么配置您的项目以使用单独的工具(Browserify、Webpack 等)捆绑解决方案,要么将SystemJS的实现包含到您的 Node.js 应用程序中(此处的说明)。

I will also end with a side note: consider evaluating whether you really need to bundle your server-sided code.我还将以附注结束:考虑评估您是否真的需要捆绑您的服务器端代码。 Bundling is typically performed in frontend projects to reduce the size of client-sided resources, although it can be done as well for server applications in resource-efficient scenarios.捆绑通常在前端项目中执行以减少客户端资源的大小,尽管它也可以在资源高效的场景中为服务器应用程序完成。

@E_net4 is right. @E_net4是对的。 Currently, it is not possible to build modules into a single file with the TypeScript compiler alone.目前,无法单独使用 TypeScript 编译器将模块构建到单个文件中。 This article describes how to do so with webpack where the file structure is: 本文介绍了如何使用 webpack 执行此操作,其中文件结构为:

├── index.js
├── package.json
├── src
│   ├── add-message
│   │   └── index.ts
│   ├── index.ts
│   └── upcase-messages
│       └── index.ts
├── tsconfig.json
└── webpack.config.js

webpack.config.js

 const nodeExternals = require('webpack-node-externals'); module.exports = { entry: './src/index.ts', output: { filename: 'index.js', // <-- Important libraryTarget: 'this' // <-- Important }, target: 'node', // <-- Important module: { rules: [ { test: /\\.tsx?$/, loader: 'ts-loader', options: { transpileOnly: true } } ] }, resolve: { extensions: [ '.ts', '.tsx', '.js' ] }, externals: [nodeExternals()] // <-- Important };

tsconfig.json

 { "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "./", "noImplicitAny": true, "strictNullChecks": true }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] }

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

相关问题 如何将 TypeScript 代码编译成一个可以通过“node mycode.js”执行的 single.js 文件? - How do I compile TypeScript code into a single .js file, executable via "node mycode.js"? 如果我的文件是正在运行的文件,我如何使node.js执行一些代码? - How do I make node.js execute some code only if my file is the running file? 如何使用Node.js自动编译.coffeescript? - How do I auto-compile .coffeescript with Node.js? 如何将我的 Typescript Node.js 应用程序部署到 Heroku? - How do I deploy my Typescript Node.js app to Heroku? 如何让我的 node.js 与我的 Visual Studio 代码一起使用? - How do I get my node.js to work with my visual studio code? 我的 node.js 代码中没有任何输出,我该如何编辑它? - I am not getting any output in my node.js code how do I edit it? 当我的代码在堆栈跟踪中没有任何地方时,如何调试node.js错误? - How do I debug node.js errors when my code is nowhere in the stack trace? 如何在node.js中设置生产代码和本地代码? - How do i setup my code for production vs local in node.js? 如何阻止Node.js / Express提供我的应用程序的源代码? - How do I prevent Node.js / Express serving up my application's source code? 如何在 Node.JS 项目中正确调整我的代码? - How do I properly modulate my code in a Node.JS project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM