replicate `npm pack` and allow for streaming to stdout - npm

The npm pack command only allows you to write out a file to the current working directory.
I am looking for a way to do this:
npm pack > $myfile
So I am considering writing my own version of npm pack - but I dont quite know how it's implemented.
Does anyone know if npm pack as its currently implemented bundles node_modules? is there some quick and dirty way to replicate its functionality but write output to stdout?
I assume npm pack is based on the tar command and that the tar command allows you to write to stdout...

You can use yarn instead of rewriting npm
yarn pack -f some/other/path.tgz
or if you really need to redirect:
yarn pack -f /dev/stdout > some/other/path.tgz

Related

Visual Studio Code - NPM not found when using NPM SCRIPTS

If I open the integrated terminal and run npm -v it works.
But, if I running it directly from NPM SCRIPTS it doesn't work.
MORE DETAIL AS REQUESTED IN COMMENT
// Edit by Fogmeister
I also have this problem, here is some more detail...
I have a test script defined in package.json...
When I run this script from the root folder command line using npm run test it works...
This script appears in the list of NPM Scripts in VS Code...
When I run from the NPM Scripts explorer it fails...
Even though it says it is running from the same folder.
Try doing, use dnf or apt-get
sudo apt-get/dnf install npm
The npm file should be in /usr/local/bin/npm. If it's not there, install node.js again with the package on their website.

Replicating `npm pack` behavior

I am trying to replicate the behavior of npm pack because it has a limitation where it does not write to stdout, it can only write to a local file (see this issue: https://github.com/npm/npm/issues/12039)
Now, I can tar the current directory and write to stdout like so:
tar --exclude='./node_modules/' -cv . | gzip > archive.tar.gz
however, when you extract npm pack tarballs, all the contents of a your package should be in a directory called 'package'.
So my question is - how can I tarball the current directory, but put the current directory inside a directory called 'package' before it gets archived?
Is there some tar -flag that lets you do that?
I did some legwork and as far as my testing goes, npm will accept a tarball with everything in the root, or everything in a subdirectory called 'package'.
To test the above theory, you can tar a NPM project directory with:
tar --exclude='node_modules' -c . > archive.tar
then install it somewhere else with
npm install /path/to/archive.tar
you can't install in the same project though, NPM will complain about circular deps, so install it in another project.

Can I get npm / gyp to use ccache?

ccache is a fantastic way to speed up building C binaries which you have already built previously, by caching the results. (Another great tool is distcc, which will pass code to other machines for parallel compilation!)
Can I get npm to use ccache when it build C files using gyp?
Here is a way to test:
$ time npm install mmmagic
...
npm install mmmagic 103.83s user 9.06s system 100% cpu 1:51.84 total
$ rm -rf node_modules/mmmagic
$ time npm install mmmagic
...
npm install mmmagic 103.48s user 8.59s system 102% cpu 1:48.87 total
If we can use ccache, it should be significantly faster on the second attempt.
Another way to see if ccache is being called, and if it is helping, is to run this in a separate terminal while a build is underway:
$ watch -d ccache -s
This will display a live update of ccache's statistics.
You should be able to do this by setting your environment variables correctly.
For a C compiler: export CC="ccache gcc" (or export CC="ccache clang") should work fine.

Yeoman can't recognize a generator that was installed globally

I ran sudo npm install -g generator-flask and it was saved. I was able to confirm by running npm list-g.
I then run yo flask. Thinking that this was going to work, I instead get an error message:
Error flask
You don't seem to have a generator with the name flask installed.
You can see available generators with npm search yeoman-generator and then install them with npm install [name].
To see the 22 registered generators run yo with the `--help` option.
I do as it tells me by running yo --help and discover that the flask generator is not there.
I try this with sudo npm install -g generator-angular-flask and the problem is recreated with Angular Flask. I'm assuming this is going to happen for all the generators which is why I've titled it as Yeoman not recognizing new npm installs.
Does anyone know why this has happened?
A workaround, but does solve the problem:
npm link generator-angular-flask
(as does just installing the module locally)
Usually it is a $NODE_PATH issue, but rather then guessing, run:
yo doctor
and follow the instructions!
Node path is the issue.
To setup path:
export NODE_PATH=$HOME/.node/lib/node_modules/
In my case node is in HOME directory.
I was having a similar issue. Basically Yeoman was showing No installed generators, even if I fired up yeoman and installed the generator again still no luck to run it.
I typed yo doctor and did the changes it recommends, but still no luck...
I then ran
export NODE_PATH=$HOME/.node/lib/node_modules/
Then I fired up Yeoman and still no luck.
I then decide run yo doctor again and voila! New errors. I then saw the final recommendation by the yo doctor so I decide to run it:
export NODE_PATH=$NODE_PATH:/Users/slickstyles/.npm-global/lib/node_modules
After I type yo and guess what I see? All my installed yeoman generators showing and working marvelously.
You can use:
yo doctor
and you will not something like:
npm root value is not in your NODE_PATH
[Info]
NODE_PATH = /home/action/.node/lib/node_modules/
npm root = /home/action/.parts/lib/node_modules
[Fix] Append the npm root value to your NODE_PATH variable
Add this line to your .bashrc
export NODE_PATH=$NODE_PATH:/home/action/.parts/lib/node_modules
Or run this command
echo "export NODE_PATH=$NODE_PATH:/home/action/.parts/lib/node_modules" >> ~/.bashrc && source ~/.bashrc
and try again:
npm install -g generator-webapp
Check if you have NODE_PATH set. To set it open a terminal and type (assuming you have node under /opt/node/ and use bash shell)
echo "export NODE_PATH=/opt/node:/opt/node/lib/node_modules" >> ~/.bashrc && . ~/.bashrc
Once done, run yo doctor. If all is ok, run yo to see your installed generators.

Running npm command from expressjs?

Can i run something like: npm install passport from expressjs itself?
I want to check if i'm using a non installed module and install it before running the application.
To answer your question, yes, you should be able to check the FileSystem for specific files/folders using fs. I imagine you would need to parse your package.json and check the local node_modules or the global install path.
I would recommend a shell script to run npm install and then fire up your app from within the same script.
npm install .
node <server> &