I had exactly the same problem.
I decided this not by copying the files, but by copying the files I need into the ./dist/ folder, and then using npm publish ; Then NPM sees this folder as a complete package, and everything works very well. The only files that I needed to copy from the root folder were:
Since we are going to copy these files to the ./dist/ folder before publishing, we DO NOT want the package.json file to refer to ./dist/ . Therefore, completely delete the package.json files entry, because we do not need to specify which files we will use - we are going to take everything in the ./dist/ folder. I use TypeScript, so I also have a typings entry and again a link to ./dist/ .
{ "name": "my-package", "version": "0.0.9", "main": "index.min.js", "typings": "index.d.ts", "private": false, "dependencies": {}, "devDependencies": {}, "repository": " git@github.com :username/my-package.git" }
Now for the publishing phase. I created a gulp task that will perform the publication for me, making it beautiful and automated (except for increasing the package version #).
From gulp I am using Node spawn () to start the npm process. However, since I actually work on Windows, I used " cross-spawn " and not the usual built-in Node.js spawn (which I learned, this did not work when I had spaces in my path!).
Here is my gulp file with TypeScript removal:
var gulp = require('gulp'); var del = require('del'); var spawn = require('cross-spawn'); // WAS: require('child_process').spawn; var config = { src: { tsFiles: './src/**/*.ts' }, out: { path: './dist/' } } gulp.task('clean', () => { return del('dist/*'); }); gulp.task('build', ['clean'], () => { .... }); gulp.task('publish', ['build'], (done) => { // Copy the files we'll need to publish // We just use built-in gulp commands to do the copy gulp.src(['package.json', 'README.md']).pipe(gulp.dest(config.out.path)); // We'll start the npm process in the dist directory var outPath = config.out.path.replace(/(\.)|(\/)/gm,''); var distDir = __dirname + '\\' + outPath + '\\'; console.log("dist directory = " + distDir); // Start the npm process spawn('npm', ['publish'], { stdio:'inherit', cwd:distDir } ) .on('close', done) .on('error', function(error) { console.error(' Underlying spawn error: ' + error); throw error; }); });
Notice when we call spawn (), we pass the third argument, which is the parameters. The main entry here is cwd:distDir , which states that spawn starts the npm process from the directory. / dist /. Because using spawn can cause problems that I encountered in handling occurrence errors. Since I was trying to fix the problem using spawn() , I found the following article fooobar.com/questions/33674 / ... very useful.
It worked like a charm; my published package has all the files in the root directory, and the ./dist/ folder ./dist/ not published.