Create pseudo node_modules folder on npm install - node.js

I updated some libraries within my project and now I get an issue with the zlib within ng2-pdf-viewer.
There is also an issue created on github, that helped to fix it for now:
https://github.com/VadimDez/ng2-pdf-viewer/issues/322#issuecomment-389281242
The solution suggests to create a zlib folder within node_modules and add an empty index.js and a basic package.json.
This fixed the issue. But now I want to automate the process, since every time anyone runs npm install the pseudo folder is gone again.
Is there any way to create this folder with it's content automatically on npm install (maybe with mkdir or something)?
PS: The solution to put "browser": { "zlib": false } into the package.json didn't help in my case...

You should be able to create a script to create the directory, and then use a hook to run that script after you run npm install:
"scripts": {
"postinstall": "myCustomScriptToCreateFolder",
},
More reading:
npm Scripts Documentation
StackOverflow Post about this sort of thing

Related

NPM package - add a script to the consumer's node_modules/.bin

I'm crafting a npm package.
I want to expose a script to the consumer as soon as the package is installed, much like Typescript's tsc.
How do I do it?
Add something similar as below to your package.json:
"bin": {
"router": "dist/src/bin/router.js"
}
This will create a symlink named router in the bin folder pointing to router.js.
Read more about bin here

How to set an environment variable to be used during "npm install"?

Our coorporate network is very closed down, so a normal method of:
npm install cypress#3.4.1
Doesnt work since it is being blocked by a proxy, we need to provide the parameter CYPRESS_INSTALL_BINARYin the following way with the help of cross-env (since we have mainly Microsoft environments here).
cross-env CYPRESS_INSTALL_BINARY='\\localserver\cypress\3.4.1\cypress.zip' npm install cypress#3.4.1
This is easy to do on first install, but the problem is that everyone on the team needs to run this command. And I want it to be possible to just type npm install and they will get all requirements automatically. This is extra obvious when we want to update the cypress package, since the binary url needs to change each time.
I tried to add a preinstall script to my package.json like so:
"scripts": {
...
"preinstall": "cross-env CYPRESS_INSTALL_BINARY='\\localserver\cypress\3.4.1\cypress.zip'",
...
},
But it seems like that environment variable set by cross-env is "gone" after preinstall is finished and install begins, since cypress tries and fails to download from the web. I am okay with it being temporary, but it needs to persist over the install-command. Also seen solutions with .env files but none of those have support for the install step as far as I can see.
My current solution is to run the entire cypress installation in the preinstall step, and it works but seems uneccessary to run a double install each time.
So, what I am asking for, is a way to let a developer just run the following commands on a brand new computer and be done.
git clone ...
cd ...
npm install
How can I do that?
Same situation on my side, except that I want to prevent the installation of cypress on the local machine.
Solved it by adding a .npmrc into the root of the project and adding to version control.
Contents of .npmrc:
CYPRESS_INSTALL_BINARY=0
Since the environment variable is used on install time, the solution with cross-env was not possible, since one cannot be sure that cross-env has been already installed.
Let me know whether it helped or you have already another solution.
I ended up creating a tool that takes care of supplying the correct binary, depending on environment:
https://github.com/tomasbjerre/dictator-cypress
I have it internally at my company. When we need a new version of Cypress, we release a new version of this tool internally.
I add the tool as a preinstall script:
...
"scripts": {
"preinstall": "npx dictator-cypress#0.0.28",
...
So that it runs when someone does npm install:
...
Copy linux cypress to cypress.zip
Applying: copy linux-x64/cypress.zip to .
.npmrc should have reference to cypress binary
Up to date: .npmrc should have lines
.gitignore should include the copied zip
Up to date: .gitignore should have lines
...

npm errors and warnings in terminal

can anybody tells me what are those errors about and how to fix them?
Everytime i try to install something in node with npm these errors show up in terminalenter image description here
ENOENT stands for basically "Error, No Entry". this means it was looking for the package.json file and it couldn't find it.The fields mentioned below are also not found because they are a part of the package.json file
so create a package.json file in the current current directory using
npm init
and add the required content in the required fields
i would also recommend you to install the modules locally into the directory of the particular project using
npm install express --save
Hope my answer helps,
cheers
The error you are facing is because you do not have package.json file. Npm installs the package in a node_modules/ subfolder, but warns you that there is no package.json file. If you want to manage localy installed npm packages you should create a package.json file. Start by creating an empty folder:
$ mkdir myapp
$ cd myapp
and then create a new package.json executing
$ npm init
Answer (or skip) all questions and at the end a brand new package.json will be created.
You can get more information from the Getting started articles in npm documentation: Working with package.json

Package and distribute Bash script with NPM to allow installation globally into developer workstation's paths with npm install

I have some scripts that I want to distribute with npm for developers to be able to install globally on their workstations and then use the commands of the scripts on their computers in their development workflow.
I can't work out how to get npm to actually add the script in its package to the path though.
I see that the firebase tools have this in their package.json:
"preferGlobal": true,
"bin": {
"firebase": "./bin/firebase"
},
...but I can't quite work out how this relates to my project.
The first project I am trying to distribute with npm is for controlling a Belkin WeMo light switch, it includes an executable 'wemo' and an included functions.inc.sh file, this can be seen # https://github.com/agilemation/Belkin-WeMo-Command-Line-Tools.git
If anyone can point me in the right direction it will be really appreciated!!!
Thanks,
James
Any key/value pairs placed into the bin key of the package.json will be symlink'ed into the NPM bin-dir path.
The key is what you want the command to be named and the value part is the script in your package it should run.
Ergo, in your example, when npm install finishes running it'll create a symlink from [package-install-path]/bin/firebase to /usr/local/bin/firebase (or whatever bin directory prefix NPM is using (npm bin -g will tell you where this is).
If you only have one script you can also do:
{
"name": "my-awesome-package",
"bin": "./myscript.sh"
}
And it'll symlink myscript.sh to my-awesome-package
Although you should be wary of including bash scripts since they won't work on Windows.
Here are the docs for this.

NPM: Change the `bin` output directory for the node modules

Currently, if you are using a package.json file to manage your project's dependencies (whatever project it is, may it be a ruby, php, python or js app), by default everything is installed under ./node_modules.
When some dependencies have binaries to save, they're installed under ./node_modules/.bin.
What I need is a feature that allow me to change the ./node_modules/.bin directory for ./bin.
Simple example:
A PHP/Symfony app has a ./vendor dir for Composer dependencies, and all binaries are saved in ./bin, thanks to the config: { bin-dir: bin } option in composer.json.
But if I want to use Gulp to manage my assets, I create a package.json file, require all my dependencies and then run npm install.
Then, my wish is to run bin/gulp to execute gulp, but actually I have to run node_modules/.bin/gulp which is not as friendly as bin/gulp.
I've looked at package.json examples/guides on browsenpm.org and docs.npmjs.com, but none of them works, because they are here to define your own project's binaries. But I don't have any binaries, because I want to use binaries from other libraries.
Is there an option for that with NodeJS/NPM ?
You might consider adding gulp tasks to your package.json.
// package.json
{
"scripts": {
"build-templates": "gulp build-templates",
"minify-js": "gulp minify-js"
}
}
You can run any scripts specified in package.json by simply running the following:
$ npm run build-templates
$ npm run minify-js
You get the idea. You can use the gulp command inside the string without doing ./node_modules/.bin/gulp because npm is smart enough to put all scripts from ./node_modules/.bin/ into the path for that script execution.

Resources