How to "build" contents of a node application to a folder called "dist"

时间:2017-10-30 15:31:43

标签: node.js npm build

I have a node.js+express application. To deploy it to my server the partner is asking me to "build" the app into a folder called "dist" where all the files that need to be deployed to the server will exist. How can I implement such kind of a build.

Any hint or guidance would be appreciated.

1 个答案:

答案 0 :(得分:1)

您可以在package.json中创建一个执行此操作的脚本。您只需创建目录并将生产中运行应用程序所需的所有内容复制到其中,不再需要。

//package.json
{
  //...
  "scripts": {
    "dist": "mkdir -p dist && cp -R node_modules src server.js ... dist"
  }
  //...
}

以上不是跨平台兼容的。这始终是此类构建脚本的复杂部分。如果这对您来说是一个问题,我建议您考虑使用gulp等可用工具。

您还可以使用NPM生命周期挂钩作为安装的一部分自动执行此操作。确保您还运行npm install --production而非npm install以省略您的开发依赖项。