Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change root directory on npm publish

Is it possible to change the root directory of my Node.js npm package when I do a publish? What I have is following:

├── lib
├── package.json
├── .npmignore
├── src
│   ├── index.js
│   └── sub
│       └── mymodule.js
└── test

In /src is all of my ES2015 source code. I transpile that with Babel into my /lib directory. The main in my package.json points to lib/index.js. After that I can make a npm publish (my .npmignore is set to src so only lib will be published). Now my package is deployed to npmjs.com and in another package I can import it with import index from 'mypackage'.

So far so good. But what if I want to import sub/mymodule.js directly? I have to write import mymodule from 'mypackage/lib/sub/mymodule'. What I really want is import mymodule from 'mypackage/sub/mymodule' without /lib/ in my path.

How can I achieve this? The npm documentation mentioned directories.lib that I can set in package.json but it seems it will be completely ignored. I set it to ./lib but it does nothing.

like image 409
LongFlick Avatar asked Aug 19 '16 05:08

LongFlick


People also ask

Does npm publish overwrite?

By default npm will publish to the public registry. This can be overridden by specifying a different default registry or using a scope in the name (see package. json ).

What happens on npm publish?

When you run npm publish , npm bundles up all the files in the current directory. It makes a few decisions for you about what to include and what to ignore. To make these decisions, it uses the contents of several files in your project directory.


1 Answers

You can publish your /lib directory instead with npm publish lib. Remember to copy your package.json file to the lib directory before publish.

A suggestion is to use npm scripts:

"scripts": { "build": "babel src --out-dir lib", "prepublish": "npm run build && cp ./package.json lib" }

like image 188
Einar Löve Avatar answered Sep 18 '22 13:09

Einar Löve