简体   繁体   中英

Do I need to bundle files when publishing a library to npm?

My project structure very very roughly looks like this:

dist/
  - helper.compiled.js
  - entrypoint.compiled.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.compiled.js can just require helper.compiled.js . What is the benefit of providing a single index.js file?

What is the recommended 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.

The best way to bundle just the compiled 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.compiled.js",
    "scripts": {},
    "author": "",
    "license": "ISC",
    "dependencies": {},
    "files": [
        "dist"
    ]
}

You need only to publish the compiled files. If you compile 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 command essentially just creates a package from your files and uploads it to the npm registry. After running the command you will be able to find the npm package and use it as any other package.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM