0

I want to unpack Node.js (Linux Binaries (x64)) to a custom folder and use it for a few projects.

The difficulties appear when I start using console. I can't use npm at all and I've got no idea how to install global modules in this case.

For instance, I install gulp -g and even specify the installation path for it, but when I wish to use gulp, it doesn't work.

So, again, I do not use package managers and /usr/lib folders, the whole node.js package in one, single folder. How to use it that way if possible at all?

There may be a few more Node.js versions there and it would be greate to have a way of setting up the /paths/variables for the selected /folder/version of Node.js.

CodeGust
  • 141
  • You will have to add that directory to your PATH. – Jared Smith Mar 06 '20 at 21:03
  • @JaredSmith thanks! not sure yet, gotta try it out later. that PATH variable must ONLY be related to the node.js path and nothing else – CodeGust Mar 06 '20 at 21:15
  • not sure at all what that means? PATH is a colon-delimited list of directories, the way you add to it is usually by tacking on a dir at the front: PATH = /path/to/node.js/bin:$PATH – Jared Smith Mar 06 '20 at 21:17
  • @JaredSmith ok, thanks. I'll post the results here as soon as I try it. Though the question here is how to tell npm that one or another folder is the current working folder of node.js and also tell it where to install the global modules. – CodeGust Mar 06 '20 at 21:20
  • You don't get to tell it where to install global modules. What you seem to be asking for is how to use node.js without installing it globally on your system, which is a more complex topic than just modifying PATH. Easiest way would be to use a virtualization solution like vagrant or docker. – Jared Smith Mar 06 '20 at 21:24

1 Answers1

0

You do not mention how you would like to manage the versions, I will illustrate a simple method using symlinks. There are packages that can manage the versions for you, such as the Node Version Manager if you would rather use those.

Starting with a node directory in my user's home, I have populated it with two versions of node:

$ tree -L 3 node
node
|-- node-v12.16.1-linux-x64
|   |-- CHANGELOG.md
|   |-- LICENSE
|   |-- README.md
|   |-- bin
|   |   |-- node
|   |   |-- npm -> ../lib/node_modules/npm/bin/npm-cli.js
|   |   `-- npx -> ../lib/node_modules/npm/bin/npx-cli.js
|   |-- include
|   |   `-- node
|   |-- lib
|   |   `-- node_modules
|   `-- share
|       |-- doc
|       |-- man
|       `-- systemtap
`-- node-v13.10.1-linux-x64
    |-- CHANGELOG.md
    |-- LICENSE
    |-- README.md
    |-- bin
    |   |-- node
    |   |-- npm -> ../lib/node_modules/npm/bin/npm-cli.js
    |   `-- npx -> ../lib/node_modules/npm/bin/npx-cli.js
    |-- include
    |   `-- node
    |-- lib
    |   `-- node_modules
    `-- share
        |-- doc
        |-- man
        `-- systemtap

You would then make a symbolic link to the version you would like to use:

$ ln -s node-v12.16.1-linux-x64 node/nodejs

and finally, we need to link all up this through your PATH:

$ export PATH=~/node/nodejs/bin:$PATH

Now, you should be able to run node commands:

$ node --version
v12.16.1
$ npm --version
6.13.4

You can switch the versions by changing the symbolic link:

$ unlink node/nodejs
$ ln -s node-v13.10.1-linux-x64 node/nodejs
$ node --version
v13.10.1
$ npm --version
6.13.7

Unfortunately, this does not handle global packages, as all versions would install/use global packages from the same place. If you have two users attempting to use different versions of node, having global packages can break things.

  • Thank you! I've just tried it and export PATH=~/pr/node.js/node-v12.16.1-linux-x64/bin:$PATH and bash: npm: command not found shows up on npm --version How will it know which node/npm version to use if I add a number of them to the PATH ? – CodeGust Mar 09 '20 at 22:44
  • I downloaded the linked tar.gz in your original question and it had npm available. Did you make sure to extract the contents into the correct directory? What is the output of ls -l ~/pr/node.js/node-v12.16.1-linux-x64/{bin,lib/node_modules}? You will not be able to have multiple versions with the same name, you would just get the first match from $PATH. – GracefulRestart Mar 10 '20 at 00:20