Is there a way to list what `npm publish` will actually publish? - npm

The docs explain how to control which files are not sent to the npm registry when you run npm publish.
If you use .npmignore, or you're not using git that set of files differs from the set that are pushed to your source repo.
Is there a way to list the files that npm publish will send?
I know that npm pack will create a tarball that contains those files, but creating a tarball and then listing its contents seems a little clunky.

Currently, there's no such thing in npm (see this issue).
At the moment you can use some external tools that implement the functionality you're asking for, e.g. pkgfiles or irish-pub.

As of at least npm#6, you can run npm publish --dry-run to see what files are included in your package.
https://docs.npmjs.com/cli/v6/commands/npm-publish

Related

How can i prevent NPM to delete locally installed modules from nodes_modules

I have some local modules which are inhouse developed and I copy to my node_modules folder manually.
When I do this they work fine but after I install some other stuff via ng add or npm install the folder is removed. My question is how can I prevent this from happening so I don't have to copy the files again ?
You need to specify your dependencies in package.json or else you cannot rely on them being in node_modules. Various npm commands might remove it, notably npm ci but also others.
If your package is not publicly published, some options are:
Use a non-public registry and publish it there.
Publish it as a scoped package with limited visibility. You will need a paid or organization account on npm for this. Individual accounts are US$7 a month.
Use npm link to "install" it from your local file system.
Use a postinstall or other life cycle script to have npm copy in your packages for you each time after npm ci or npm install is run.
There are likely other options, but those are the ones that come to mind immediately.

How to prevent npm install removes local added packages

Looked to numerous SO questions and answers, however have not found the answer to fix the following issue.
On my local machine I run npm from the folder node_modules. This folder contains several packages from GIT-sources, however there are two folders containing custom made scripts. Each time I update package with npm install my two custom folders are gone. Luckily I made a backup and was able to restore these two. But it's also annoying, because what else is removed that I don't see??
I'm on npm version 6.13.2
Any help is of course highly appreciated.
There is no way you can prevent npm from removing those folders because they are not in your package.json.
Although there are two things that you can do which are as follows,
Create npm modules for those 2 scripts and update their entries in your package.json. Here is a link for 'how to create and publish node module'.
https://www.guru99.com/node-js-modules-create-publish.html
Create a repo for those 2 scripts and update their git paths in your package.json.

Does npm or yarn clone from VCS and run build script when install a package?

I am studying about npm and I have some questions.
Where the npm get the package from? i.e. when run npm install <package-name> or yarn add <package-name>.
When get the package, do npm get the package as raw or get then build it(like run the build script written in package.json)?
When publish the package, the repository field of package.json is required?
Can be different between the repository for publishing and the repository in pacakge.json?
To answer your questions:
npm gets them from the NPM package registry, and so does yarn, but Yarn probably has a proxy registry in front of it. In general, you can say, both tools fetch their packages from https://npmjs.com by default.
It gets the package as it was published (so, in short, the answer is "raw"). Building is up to the publisher and depends on the type of package. Often, some prepublish task builds something into dist/ (or any other location in the package), and these files are also shipped with the package others then download. Building rarely happens after installing a package (exception here are library-wrapping packages built with node-gyp).
The repository field is not required, to my knowledge, but it is good practise to include it (it will be displayed on the NPM website, for example).
Technically, yes. You can just specify any repository in repository, but it wouldn't make much sense to specify one that isn't the source of the package.
If you in general want to read up more on how npm works, check out it's documentation over at https://docs.npmjs.com/

How to convert npm-shrinkwrap.json to package-lock.json file?

I have an old project with npm-shrinkwrap.json file.
Now, I want to convert npm-shrinkwrap.json to package-lock.json file.
How can I do that?
The most important thing is the dependency tree and version must be correct.
As a simple solution, we may just rename file from npm-shrinkwrap.json to package-lock.json since both the files are meant to have exactly same content and format. Done! Refs package-lock.json and npm-shrinkwrap.json.
Although both the files have exactly the same content, there are a handful of differences in how npm handles them. For example when we run npm install vs npm ci commands. Also note that package-lock.json will not be published to npm registry unlike npm-shrinkwrap.json, if we ever decide to publish our npm package (using npm publish command). One may refer official npm docs for detailed info on these specific commands.
Cheers!

How can you invoke specific lifecyles of the npm install command?

I've got an irritating little bug with AWS, where I create my workspace (in this case, npm install) and distribute it to a bunch of slaves. Part of the install lifecycle for npm creates a bunch of symlinks in ./node_modules/.bin. Unfortunately, S3 doesn't support symlinks (see this question).
Now, it's a bit unfortunate that I'm downloading a prebuilt ./node_modules from S3, but it's how it's gotta be done (outside the scope of the question). When I run npm install on the slave, node doesn't recreate the symlinks.
I could always add a pretest hook to recreate the symlinks manually, but that's what got me wondering: is there a way to invoke specific parts of the npm lifecycle manually? If not, why? Running only parts of the install could be useful -- at least, here.