最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do I need to bundle files when publishing a library to npm? - Stack Overflow

programmeradmin1浏览0评论

My project structure very very roughly looks like this:

dist/
  - helperpiled.js
  - entrypointpiled.js
src/
 - helper.js
 - entrypoint.js

I was reading the npm publishing guidelines and it says to provide a single index.js file. But is this really necessary? Afterall, my entrypointpiled.js can just require helperpiled.js. What is the benefit of providing a single index.js file?

What is the remended procedure for publishing a library to npm in this situation? I tried using npm pack, but I don't quite understand what it is doing.

My project structure very very roughly looks like this:

dist/
  - helper.piled.js
  - entrypoint.piled.js
src/
 - helper.js
 - entrypoint.js

I was reading the npm publishing guidelines and it says to provide a single index.js file. But is this really necessary? Afterall, my entrypoint.piled.js can just require helper.piled.js. What is the benefit of providing a single index.js file?

What is the remended procedure for publishing a library to npm in this situation? I tried using npm pack, but I don't quite understand what it is doing.

Share Improve this question asked May 17, 2020 at 23:07 FoobarFoobar 8,54521 gold badges101 silver badges183 bronze badges 1
  • When publishing a library include the files or the folder in the files section of the package.json – Get Off My Lawn Commented May 17, 2020 at 23:32
Add a ment  | 

2 Answers 2

Reset to default 4

The best way to bundle just the piled files is to put the directory in the files section of your package.json. This will then only package the files that npm uses such as package.json, README.md, and other files that your package requires.

An example package.json looks something like this:

{
    "name": "my-library",
    "version": "1.0.0",
    "description": "My Library.",
    "main": "dist/entrypoint.piled.js",
    "scripts": {},
    "author": "",
    "license": "ISC",
    "dependencies": {},
    "files": [
        "dist"
    ]
}

You need only to publish the piled files. If you pile them correctly then there is no need to include your src/ folder in the package.

Here is my .npmignore file for my tree in react package: .npmignore You can see what is in the package here.

As you can see, I publish only the dist directory and every file in the root. The bare minimum is the package.json and the dist directory.

npm publish mand essentially just creates a package from your files and uploads it to the npm registry. After running the mand you will be able to find the npm package and use it as any other package.

After you run npm publish I remend downloading published the package from the registry and try out if all the required files are there and verify that you can use everything.

发布评论

评论列表(0)

  1. 暂无评论