node.js 如何发布带有分发文件的 npm 包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31642477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to publish a npm package with distribution files?
提问by Naresh
I would like to publish a npm package that contains my source as well as distribution files. My Github repository contains srcfolder which contains JavaScript source files. The build process generates distfolder that contains the distribution files. Of course, the distfolder is not checked into the Github repository.
我想发布一个包含我的源代码和分发文件的 npm 包。我的 Github 存储库包含src包含 JavaScript 源文件的文件夹。构建过程生成dist包含分发文件的文件夹。当然,该dist文件夹并未签入 Github 存储库。
How do I publish a npm package in a way that when someone does npm install, they get srcas well as distfolder? Currently when I run npm publishfrom my git repository, it results in only srcfolder being published.
我如何以一种方式发布 npm 包,当有人发布时npm install,他们会得到src和dist文件夹一样的东西?目前,当我npm publish从 git 存储库运行时,只会src发布文件夹。
My package.json looks like this:
我的 package.json 看起来像这样:
{
"name": "join-js",
"version": "0.0.1",
"homepage": "https://github.com/archfirst/joinjs",
"repository": {
"type": "git",
"url": "https://github.com/archfirst/joinjs.git"
},
"main": "dist/index.js",
"scripts": {
"test": "gulp",
"build": "gulp build",
"prepublish": "npm run build"
},
"dependencies": {
...
},
"devDependencies": {
...
}
}
采纳答案by Eugene Nezhuta
Take a look at the "files" field of package.json file https://docs.npmjs.com/files/package.json#files
看一下 package.json 文件https://docs.npmjs.com/files/package.json#files的“文件”字段
From the documentation:
从文档:
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)
“文件”字段是要包含在项目中的文件数组。如果您在数组中命名一个文件夹,那么它也将包含该文件夹中的文件。(除非它们会被另一条规则忽略。)
回答by topheman
When you npm publish, if you don't have an .npmignorefile, npm will use your .gitignorefile (in your case you excluded the distfolder).
当你npm publish,如果你没有一个.npmignore文件,故宫将使用您的.gitignore文件(在你的情况排除的dist文件夹)。
To solve your problem, create a .npmignorefile based on your .gitignorefile, without ignoring the dist folder.
要解决您的问题,请.npmignore根据您的.gitignore文件创建一个文件,而不要忽略 dist 文件夹。
Soure : https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package
来源:https: //docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

