How to npm publish specific folder but as package root

npm publish dist folder
npm publish dist folder as root
npm publish with token
npm publish new version
npm link dist folder
npmignore
create npm package and publish
npm publish locally

I have a project that include a gulp task for building and packaging the sources and release in a directory called dist. My goal is publish it as a npm package, but only my dist folder. The npm documentation says that I can use the files tag to specify files to export. It works. But, documentation also says that:

If you name a folder in the array, then it will also include the files inside that folder

The result, is a npm package, which node_modules looks like:

But I'd like to see all my files at the package's root (without that dist folder). My index.js file is inside the dist folder, but should be at root. I've tried to set tag files as /dist/**/* but it didn't work.

How can I achieve it?

I have the same desire but I think there is no way of accomplishing this with only using npm tooling. Another script/tool could be used to arrange your package.

Alternative Solution

Currently I am copying my package.json into the dist folder and then running npm pack inside the dist folder. I think this essentially provides the desired arrangement of our package.

Here is some relevant reading on this npm design: Why no Directories.lib in Node.

It's also interesting to note that jspm DOES respect the directories.lib option in package.json and rearranges the files when resolving the npm package. This all has come about for me because I am wanting to build a common library which can be consumed by jspm or npm/webpack.

Publishing flat npm packages for easier import paths & smaller , Anywho, I was trying to find a way to publish an npm package so the root directory of the project (including your built dist or lib folder) you just need nicer way for developers to include only specific files from your package  This means publishing a package involves many steps. At the very least, you need to: Run tests (if there are any) Update version in package.json according to Semver. Create a git tag according to Semver. Push the package to Github. Push the package to npm. Create release notes for every update.

If your project has git you can use small hack. Add next scripts to package.json

    "prepublishOnly": "npm run build && cp -r ./lib/* . && rm -rf ./lib",
    "postpublish": "git clean -fd",

now when you run publish command npm involve prepublishOnly. It builds files and saves them to lib folder (a build script depend on your project). The next command copies files to root folder and removes lib. After publish postpublish script returns the project to a previous state.

npm-publish, if no argument supplied Sets tag 'latest' if no --tag specified. DESCRIPTION§. Publishes a package to the registry so that it can be installed by name. If both files exist and a file is ignored by .gitignore but not by .npmignore then it will be to a gzipped tar archive containing a single folder with a package.json file inside. Pulling files from a directory into the root folder for NPM. I am publishing a library to NPM. When I build the library, the resulting artifact is placed in the dist folder located in the root of my project as index.js. When users install from NPM I would like index.js to be present in the root of the folder created in their node_modules folder.

Link and publish not the package directory but it's child `dist` folder , This is the real reason to support this, and it's sad that NPM had this feature at one point with special files denoting the root, but has since  This article is just going to cover the essentials for getting your packages published on npm. npm is included with Node.js. To check if npm is installed on your system, run this command in your terminal: npm -v. Make the Directory. Let’s create a folder that’s going to hold our package’s source code. In your terminal:

I highly recommend you to use .npmignore instead of moving or copying things around, specially if you are using a CI for deployments, and just add there the files you don't want to publish.

https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

Example:

#tests
test
coverage

#build tools
.travis.yml
.jenkins.yml
.codeclimate.yml

#linters
.jscsrc
.jshintrc
.eslintrc*

#editor settings
.idea
.editorconfig
Update:

If you want to split your code, into different npm packages using the same repo, I bumped into this project recently: Lerna and looks really good.

Maybe you should take a look

How To: Publishing Your First Package to npm, Here's a little guide for publishing your own packages on npm. You've written There's a lot of configurations for npm but many times these will be project-​specific. Let's create a folder that's going to hold our package's source code. Let's create the README file in the root of your package's directory:. However, there's no requirement that an npm package should be a module. There are CLI packages, that contain only executable command-line interface(CLI) and don't provide a main field to be loaded with require(), these type of package is not a module. In the context of a Node program, the module is loaded from a file.

This works fine for me.

cd TMPDIR; npm pack /path/to/package.json

Tarball will create inside the TMPDIR directory.

A Simple Guide to Publishing an npm Package, I tried to make this guide as simple yet as complete as possible but humans do make This guide explains how to initialize and publish a simple package to npm. If you do not have one, go ahead and create an account. Once you are done, you will see a new package.json file created in the package root directory. So I thought I'd try setting it up as a non-root user. According to npm documentation, a non-root user without root access can set up npm by: creating a .npmrc file with root, binroot, and manroot pointing to folders that the user owns. Then running the install script. OK. Install was fine. But node can't see the packages provided by npm.

How to publish packages to npm (the way the industry does things , It's not simple to publish and update packages to npm according to industry standards. Feel free to skip to the next section if you've published one before. To do so, create a folder somewhere on your computer and navigate to it. But I prefer installing np globally on my computer so I can run the np  Set up private npm server; Write a ‘HelloMessage’ React Component; Publish npm package & configuration; Local npm server. If you want to keep the components private, one way is to use the private function in npm, the other is to set a local(or private) npm server. It’s very easy to set up a private npm server with sinopia.

Step by step: Building and publishing an NPM Typescript package., Even if the one using your package doesn't use Typescript, some editors, like Visual but use a unique name so you can publish your package to npm later on . Let's create a src folder in the root and add an index.ts file: However, if you're on a VM or something, or if you can't or just don't want to use root for some other reason, then you can tell npm to do a different thing, and it'll obey you, because you are a human who can modify configurations to set up the machine how you want it.

Publishing an NPM package, Originally, I wanted to use react-tag-input but it was already taken. Thankfully This files must be located in the root folder of your package. Keep in On that note, one thing to consider is what files we should publish to NPM. Creating and publishing private packages To share your code with a limited set of users or teams, you can publish private user-scoped or Org-scoped packages to the npm registry. For more information on scopes and private packages, see “ About scopes ” and “ About private packages ”.

Comments
  • (Why no Directories.lib...) is a dead link
  • Why no Directories.lib in Node is saying, if you don't do it our way, you should feel pain. All this does is creates the need for tools to workaround this issue.
  • I believe this answer is outdated. As responses below note, using npm pack, the package.json files and main fields, and .npmignore gives a developer everything needed to create a package from a specific, installable directory.
  • @Jefftopia unless you want to publish chunks...
  • Made some scripts to streamline/enforce the "publish subdir" pattern
  • This answer looks like best one, but do you have ready solution aside theory?
  • @yumaa I've edited my answer with a concrete example. Hope it is useful.
  • We would still have to emit our built files in the root directory of the package. Perhaps permissible for CI. Observe the post from Isaac's blog I linked to and tell me how your solution would allow for require('mypackage/foo') instead of require('mypackage/dist/foo')
  • I wasn't trying to solve that problem here. If you want to split your code, I bumped into this project recently: lernajs.io and looks really good
  • Found another tool that worth have a look :) github.com/philcockfield/msync
  • ^This answer is underrated. npm pack plus the package.json files field (or .npmignore) works wonderfully.
  • Isn't this the same as what the OP originally had except changing the name from dist to lib?
  • does this do as the OP requested? I wasn't able to get this to work. The idea was to make the published package contain only the contents of the dist directory without including the directory itself. I this to makes sure we don't include anything not in the dist directory which can already be done withe package.json "files" list.