如何从基于节点模块的 JavaScript 文件创建构建?

发布于 2025-01-16 08:38:18 字数 380 浏览 4 评论 0原文

我创建了一个新的节点项目,

npm init
npm install apollo-server

然后添加了一个包含以下代码的 index.js 文件:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server.listen().then(({ url }) => {
  console.log(`
              

I created a new node project using

npm init
npm install apollo-server

I then added a index.js file with the following code:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server.listen().then(({ url }) => {
  console.log(`???? Server ready at ${url}`)
});

While I can run this with node index.js, how could I actually create a build from my index.js so it doesn't directly need the modules during runtime? (goal: deploy it on platforms like e.g. render.com)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2025-01-23 08:38:19

听起来您想创建一个不需要服务器端配置或设置即可运行的可执行工件。

为此有几种选择。您可能正在寻找 Javascript 捆绑程序,例如 Rollup包裹Webpack。 Webpack 使用最广泛,但通常也是最难配置的。

使用捆绑器

Parcel

使用 npm i -g Parcel 安装 Parcel,然后将其添加到 package.json:

  "main": "dist/index.js",
  "targets": {
    "main": {
      "includeNodeModules": true
    },
  },

然后运行 ​​parcel build index.js。就是这样!

我在 GitHub 上添加了一个简单的 演示

Webpack

对于这个问题,有很多很好的答案

Bundler 警告

许多节点包都附带二进制/本机插件。例如,Cypress 下载并安装浏览器。任何使用本机插件的包都无法与捆绑器一起使用,因为捆绑器无法添加二进制文件。这些软件包仍然需要安装。

另一种选择:构建二进制文件

在上述过程中,您的输出工件是单个 Javascript 文件。您还可以生成二进制文件,而不是 Javascript 文件,从而减少安装 Node 运行时的需要。请查看 pkg 来执行此操作。虽然 pkg 是一个相当成熟的产品,但这通常仍然被认为是一种实验性技术,因此可能不适合生产环境。另一方面,包含本机插件的软件包不应遇到任何问题。阅读文档以了解它是否适​​合您的用例。

It sounds like you want to create a single executable artifact which requires no server-side configuration or setup to run.

There are a few options for this. You're probably looking for a Javascript bundler, like Rollup, Parcel or Webpack. Webpack is the most widely used, but also generally the most difficult to configure.

Using bundlers

Parcel

Install Parcel with npm i -g parcel, then, add this to your package.json:

  "main": "dist/index.js",
  "targets": {
    "main": {
      "includeNodeModules": true
    },
  },

Then run parcel build index.js. That's it!

I've added a simple demo on GitHub.

Webpack

There are a number of great answers on this SO question.

Bundler caveats

Many node packages come with binary/native addons. For example, Cypress downloads and installs a browser. Any package which uses native addons will not work with a bundler, as the bundler is unable to add the binary files. These packages will still need to be installed.

Another option: building a binary

In the processes above, your output artifact is a single Javascript file. Instead of a Javascript file, you could also produce a binary file and thus alleviate the need to install the Node runtime. Check out pkg to do this. While pkg is a fairly mature product, this is generally still considered an experimental technology, so perhaps not suited for a production environment. On the other hand, you shouldn't run into any problems with packages that include native addons. Read the documentation to see if it's appropriate for your use case.

ˉ厌 2025-01-23 08:38:19

https://github.com/vercel/ncc 这可能会有所帮助。它在最终工件中包含 node_modules

https://github.com/vercel/ncc this might help. it includes node_modules in the final artifact

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文