最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far