Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish a dist folder in npm

Tags:

node.js

npm

I have a javascript library that I want to publish on npm: let's call it foo. The problem is that the javascript files to be consumed by the clients are under a dist folder. Therefore, for the moment the clients has to include dist in their import statement like this:

import { bar } from 'dist/foo'

I would like to allow clients to import the foo library without mentioning dist explicitly, like this:

import { bar } from 'foo'

Is there a way to add a property in package.json of foo or maybe in the package.json of the clients?

Remark: The reason why the javascript files are in a dist folder is because my library is written in clojurescript. Therefore the source files are under src and the generated files are under dist.

like image 428
viebel Avatar asked Jun 27 '18 11:06

viebel


People also ask

Does npm install create a dist folder?

It will create the node_modules directory in your current directory (if one doesn't exist yet), and will download the package to that directory. npm install has nothing to do with creating (or failing to create) the dist folder, which is what OP is asking about.

What is dist npm?

dist/ is the distribution version that has been modified to perform better for users not looking to modify how the code works.

What is dist folder in node modules?

The dist directory is not any special, but it's very common to have a dist directory that contains an UMD build. Especially as Unpkg allows you to import a node module without having to publish it manually to a CDN, it uses the dist or umd by default (as described at the bottom of the homepage).


1 Answers

You can add a main section in your package.json :

main: If you have a single module that serves as the entry point to your program (like what the "foo" package gives you at require("foo")), then you need to specify that in the "main" field.

ref: https://docs.npmjs.com/misc/developers

But it will only work for a single file, if you need to require multiple files, you should only publish your dist folder :

npm publish dist
like image 65
Gabriel Bleu Avatar answered Sep 20 '22 12:09

Gabriel Bleu