Node.js and modules on a system where I can't sudo - node.js

I would like to use node on a system on which I don't have sudo permissions.
I downloaded node and followed a guide on how to install npm packages globally without sudo permissions.
Node and npm commands works well. My problem is that installed packages (in my case bower and gulp), search for node in /usr/bin/env: node:
So if I run bower I get /usr/bin/env: node: No such file or directory
I can't create a ln in that directory because I don't have permission. How can I tell bower and gulp to search for node in another path?

My problem is that installed packages (in my case bower and gulp), search for node in /usr/bin/env: node:
They most certainly do not. Here is the first line in gulp:
#!/usr/bin/env node
It is the same in bower. Note that it does not have any of the two colons that you show.
Your question also suggest you are misinterpreting the error message. There is nothing there that implies you should have sudo capabilities. That message tells you that env is unable to find node. Let me illustrate with a name that does not exist on my system:
$ /usr/bin/env nonexistent
/usr/bin/env: nonexistent: No such file or directory
$ env nonexistent
env: nonexistent: No such file or directory
If I do env node it will start node. The error you are getting is exactly in the form of the error messages above: you get the path used to launch env, which is the full path if that's what was used, the name of the command that was not found and the description of the problem.
Why does it fail? It fails because env is not able to find node among the paths specified in your PATH environment variable.
I don't know what your guide told you but if you install locally, you either have to install in such a way that the binaries of Node will be in a path already covered by your PATH, or you should modify the file that sets your environment variables to add the paths in which node and npm were installed. For bash, you'd modify ~./profile like this:
PATH=/path/to/the/node/binaries:$PATH
I prepend to make sure that the path you want will take precedence over anything that could also be installed in the system locations (/bin, /usr/bin, etc.)

Are you able to edit the bowserrc file?
Bower can be configured using JSON in a .bowerrc file. For example:
{
"directory": "app/components/",
"analytics": false,
"timeout": 120000,
"registry": {
"search": [
"http://localhost:8000",
"https://bower.herokuapp.com"
]
}
}
The config is obtained by merging multiple configurations by this order of importance:
CLI arguments via --config
Environment variables
CLI arguments via --config
Environment variables
Local .bowerrc located in the current working directory
All .bowerrc files upwards the directory tree
.bowerrc file located in user’s home folder (~)
.bowerrc file located in the global folder (/)
Found in:
http://bower.io/docs/config/

Related

NodeJS - npm install practice

Created new folder and did npm install serve in it.
It created package-lock.json and node_modules/ folder.
When I run in the same folder serve it shows error:
command not found: serve
What is the way to install?
I am using: npm#6.5.0
My dev environment is MACOS
I read a great many pages on this topic and nothing worked until I tried the following
./node_modules/.bin/serve -s build
Also if you are using VS CODE you may want to bring up the terminal window outside of VS CODE - this seems to have snared a lot of people.
First of all, you should start your project running
npm init
This will create the package.json file.
Then, you can install the serve package globally.
npm install -g serve
And now you can run serve.
The serve binary was not found because the operating system cannot locate it in the PATH environment variable.
When you do the npm install serve command. The serve module is only installed into the node_modules directory found under the the project folder. Unless you explicitly include the absolute path of this node_module directory as part of your PATH env var, the OS won't know where to find serve.
Like others say, the typical practise would be to install the module using the -g flag. G means global.
When -g is used, npm will put the binary in its node directory somewhere and this this directory would have been included as part of your PATH when you install node, thus making the any new binary discoverable.
If the node.js module has a "command" and you want to run it without installing the module globally(npm install -g serve). You can run it like ./node-modules/.bin/command from the root folder of the project.
Now, what is generally used is npx, so that you can from within a project easily run any of the binaries within its local node_modules or your system's global node_modules/ and any other commands on the $PATH.
For example, here we install webpack as a local dependency. You can image doing this in a folder after running npm init. Then we run webpack without having to worry about referencing the bin file:
$ npm i -D webpack
$ npx webpack

npm webpack: coomand not found

I'm running this command on linux:
sudo npm install -g webpack
I'm getting the following output:
/home/igor/.npm-global/bin/webpack ->
/home/igor/.npm-global/lib/node_modules/webpack/bin/webpack.js
/home/igor/.npm-global/lib └── webpack#1.13.3
which looks perfectly legit, but when I try to use:
webpack -h
I get the
webpack: command not found
How can I make webpack running from the command prompt?
The default directory for globally installed NPM modules is /usr/local, which will install the module binaries inside bin folder.
If you echo your $PATH environment variable you'll see that /usr/local/bin is in your path. That means when you run a command like webpack, macOS will try to find the binary in this folder or any other folder on your $PATH.
At some point you probably changed it to ~/.npm-global, which installed webpack binary into your /home/igor/.npm-global/bin/. As this folder is not in you $PATH, macOS did not find it. You can run npm config get prefix to confirm this.
Solution 1 is to add it to your path by changing your ~/.profile file. Just append export PATH=~/.npm-global/bin:$PATH to it and restart your terminal.
Solution 2 is to change back the default folder to /usr/local by running npm config set prefix '/usr/local'. In this case you won't need to change your PATH variable.
Try to install webpack localy on the path you're working with.

How to make bin script available for npm package installed locally

I read on the npm documentation that you can't use bin scripts of locally installed packages.
So, how gulp can be launched as bin command when installed locally?
What's making it available when locally installed, I reviewed the gulp package.json and the bin scripts, I don't found any answer.
From NPMJS documentation:
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
So, your locally installed packages binaries will be executable like this
./bin/node_modules/.bin/the_binary
This is if you want to launch the binary directly. Or, as specified in the scripts part of the documentation:
In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts.
Thus, you can simply write a wrapper script like
scripts: {
"build": "the_binary"
}
and call your script like this
npm run build
Bonus
As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:
npm run test -- --grep="pattern"
You can use lpx https://www.npmjs.com/package/lpx to
run a binary found in the local node_modules/.bin folder
run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace
lpx does not download any package if the binary is not found locally (ie not like npx)
Example : lpx tsc -b -w will run tsc -b -w with the local typescript package

How to run a node.js app from name

How do you create a console app that you can run by a name instead of using node and a .js file to start the app?
Instead of...
$ node myapp.js --out ./folder/
Can I...
$ myapp --out ./folder/
You can use the bin option in your package.json file to make your script executable. From the documentation for package.json:
A lot of packages have one or more executable files that they'd like
to install into the PATH. npm makes this pretty easy (in fact, it uses
this feature to install the "npm" executable.)
To use this, supply a bin field in your package.json which is a map of
command name to local file name. On install, npm will symlink that
file into prefix/bin for global installs, or ./node_modules/.bin/ for
local installs.
Click the link and search for bin: https://www.npmjs.org/doc/json.html for more info.

Modules are not installing globally with "npm -g install..."

On Ubuntu 12.04 x 64...
npm -g install hiredis redis
Installs fine and npm ls shows those modules, but only when I'm in node source directory
does not show when I'm in any other directory
For kicks, tried running the command while in that other directory- still no dice :(
They are installing globally, but you cannot see them with npm ls, in other directories. Because npm ls only shows local modules. If you want to list global modules you have to type: npm ls -g.
Sometimes another version or just a wrong path is referenced in the npm config file instead of the installed version.
This may cause node/npm to misplace global modules.
To check and fix:
In cmd line type: npm config list
You should get a list of configuration values, one of them is prefix.
Make sure the path in prefix is the same path (only without node.exe) as the actually installed node.exe path.
(this path is listed further down as node bin location)
If it's not, change it:
Either in the config file (in your user folder, named .npmrc)
Or, via cmd line: npm config set prefix "C:\Program Files\nodejs" (change path for ubuntu of course)
Reinstall the module/package you tried to install, don't forget -g for global.

Resources