Jenkins using old node version to build project - node.js

I am working on an existing job that builds a project using node but it looks like it will need to run on v8.11.1 specifically.
here are the following commands we use on Jenkins:
source ~/.profile
echo 'Install required packages'
npm install -g bower gulp nodemon
npm install
bower install
echo 'Building production code'
gulp build
It build our project successfully but only uses version 6.11.2
/home/jenkins/.nvm/versions/node/v6.11.2/bin/bower -> /home/jenkins/.nvm/versions/node/v6.11.2/lib/node_modules/bower/bin/bower
/home/jenkins/.nvm/versions/node/v6.11.2/bin/gulp -> /home/jenkins/.nvm/versions/node/v6.11.2/lib/node_modules/gulp/bin/gulp.js
/home/jenkins/.nvm/versions/node/v6.11.2/bin/nodemon -> /home/jenkins/.nvm/versions/node/v6.11.2/lib/node_modules/nodemon/bin/nodemon.js
I have tried downloading node v8.11.1 to the /home/jenkins/.nvm/versions/node/, copying gulp, nodemon and bower to the bin directories and using nvm alias default v8.11.1 to switch my node versions
Though, when I run the project it always runs on the older version. What could I do to force it to run to the 8.11.1 version?

You may need to restart Jenkins for it to start using/recognize the newer version of node.

Related

How to install ember-cli version 1.13.11 without global?

I just install nvm software and I will run nvm install 4.0.0. The node will download and npm also download and install it automatically. Next, I will run nvm use 4.0.0 set-up will set it. now I see my node version using node -v it shows v4.0.0 all are correct all comments under admin only and I will create one folder and change to that path and I will run npm install ember-cli#4.0.0 it will install it successfully but after that I use ember -v it shows ember is not recognized?

Upgraded node and npm via nvm, but old node is still used for global packages

I've been using node 9.3.0 for a long time, but I recently migrated to 10.12.0. Everything went fine, when I do node -v and npm -v I get the correct versions:
Paul-Bergs-Macbook:node paulrberg$ node -v
v10.12.0
Paul-Bergs-Macbook:node paulrberg$ which node
/Users/paulrberg/.nvm/versions/node/v10.12.0/bin/node
Paul-Bergs-Macbook:node paulrberg$ npm -v
6.4.1
Paul-Bergs-Macbook:node paulrberg$ which npm
/Users/paulrberg/.nvm/versions/node/v10.12.0/bin/npm
Howeven, when I'm trying to run any npm command, the old version is used. That is:
Paul-Bergs-Macbook:node paulrberg$ npm i truffle -g
/Users/paulrberg/.nvm/versions/node/v9.3.0/bin/truffle -> /Users/paulrberg/.nvm/versions/node/v9.3.0/lib/node_modules/truffle/build/cli.bundled.js
+ truffle#4.1.14
added 81 packages from 311 contributors in 1.715s
And:
npm list -g --depth=0
/Users/paulrberg/.nvm/versions/node/v9.3.0/lib
└── truffle#4.1.14
Not sure if this is some bash code still pointing to the last version, but I can't seem to find any proof for that. Running env and checking for 9.3.0 environment variables yields no result.
What I did so far:
Delete node 9.3.0 with nvm uninstall 9.3.0
Do a fresh install of nvm after deleting it and rebooting the computer
nvm reinstall-with-packages
Deleted ~/.nvmrc and set 10.12.0 in there afterwards
Check if I have an overlapping node from homebrew and I don't
What could the problem be?
After a few hours of painful Unix debugging, I realised the problem was that I set a prefix in npm config:
prefix = "/Users/paulrberg/.nvm/versions/node/v9.3.0"
To fix this, make sure to unset the prefix by doing:
npm config rm prefix
This is mentioned, albeit not necessarily shining in the nvm doc
If you have an ~/.npmrc file, make sure it does not contain any prefix
settings (which is not compatible with nvm)
It looks like you might need to run nvm reinstall-packages
https://github.com/creationix/nvm#migrating-global-packages-while-installing
which says
Migrating global packages while installing
If you want to install a new version of Node.js and migrate npm packages from a previous version:
nvm install node --reinstall-packages-from=node
This will first use "nvm version node" to identify the current version you're migrating packages from. Then it resolves the new version to install from the remote server and installs it. Lastly, it runs "nvm reinstall-packages" to reinstall the npm packages from your prior version of Node to the new one.
You can also install and migrate npm packages from specific versions of Node like this:
nvm install 6 --reinstall-packages-from=5
nvm install v4.2 --reinstall-packages-from=iojs
The other "solution" is not to use global packages. Particularly when using nvm and not being able to be sure that the global package is for the "current" version it can be better to install locally and use npx to run the local command
truffle installs a truffle command to ./node_modules/.bin when you npm install it so you can npx truffle to run the local one instead of truffle to run the global one
edit:
another thing to check is that node -v and nvm current don't necessarily report the same version.
I wonder if nvm current would report v9.3 for you?
ah, yep, on my machine I can install truffle globally in a different location than node -v reports
> node -v
v9.5.0
> nvm current
system
> nvm use v8
Now using node v8.4.0 (npm v5.3.0)
> node -v
v8.4.0
> nvm current
v8.4.0
> npm install -g truffle
/Users/pauldambra/.nvm/versions/node/v8.4.0/bin/truffle -> /Users/pauldambra/.nvm/versions/node/v8.4.0/lib/node_modules/truffle/build/cli.bundled.js
+ truffle#4.1.14
added 81 packages in 4.364s
So you might be missing an nvm use v10 command
I think that a more permanent solution is this section from the support docs.
Default global packages from file while installing
If you have a list of default packages you want installed every time you install a new version, we support that too -- just add the package names, one per line, to the file $NVM_DIR/default-packages. You can add anything npm would accept as a package argument on the command line.

'npm install --only=dev' deletes existing packages

While working on a node project, I am having trouble separating installation of application and test dependencies using npm. I am using node version 8.1.2 and npm version 5.0.3.
To elaborate, I am using docker to create production and test images for my node application with the idea that the production image will have only the application dependencies installed (e.g. aws-sdk, xml-builder). I am doing this by running npm install -q --only=prod in the production docker image.
The test image extends the production image and installs the test dependencies (e.g. chai, mocha) on top of it. This is achieved by running npm install -q --only=dev command in the test docker image. The purpose of this exercise is to create a clean production image that doesn't have unnecessary packages.
But when I execute the latter npm command (npm install -q --only=dev) it removes the packages installed by the first install. I'm doing the same thing in another project that uses an older version of npm and node and it works fine.
Did something change in the latest version of npm? If so is there another prescribed way of achieving the same effect?
It's not because of the new version of Node.js, but because of the new version of npm that is bundled together (version 5).
In your case you could do one of these:
Execute first npm install --only=prod in your production image, and then just npm install in your test image.
Deleting package-lock.json after the first npm install.
Using the option --no-package-lock in each npm install.
The new npm version uses a new file called package-lock.js, the one producing this behaviour, more info here.
EDIT:
I just found out this is an issue with npm, it seems it will be fixed in the next release. At the moment the workarounds I wrote above should work.

How do I copy global modules between Node installations using nvm?

I manage my Node installations using nvm. I installed the grunt-cli module globally in my previous installation of node. This allowed me to run the command grunt on in the terminal.
When I installed a new version of Node (5.7.1), I got this error whenever I tried to execute the grunt command:
zsh: command not found: grunt
I discovered that the grunt-cli package had not been installed for the new version of Node.
I could install the grunt-cli package again but I would prefer to do this automatically whenever a new version of Node is installed using nvm.
Is there some way to install all the global modules from a previous version of Node when using nvm ?
This can be achieved using the --reinstall-packages-from option when executing nvm install. From the nvm documentation:
If you want to install a new version of Node.js and migrate npm packages from a previous version:
nvm install node --reinstall-packages-from=node
This will first use "nvm version node" to identify the current version you're migrating packages from. Then it resolves the new version to install from the remote server and installs it. Lastly, it runs "nvm reinstall-packages" to reinstall the npm packages from your prior version of Node to the new one.
If your prior version of Node is 4.3.0, the command will be executed thus:
nvm install v5.7.1 --reinstall-packages-from=4.3.0

Installing Grunt, osx, nvm, zsh

Installing Grunt
I have osx yosemite, zsh, nvm. The version of node I am using is v0.10.33.
I installed node in a way I do not need to use sudo (node and npm reside in my home folder)
here where I export the global dependencies in my zshrc
export NODE_PATH=/Users/nickname/.node/lib/node_modules/:/Users/nickname/.node/lib/node_modules
Just for information, I am trying to install Ghost (the blogging plataform).
I cloned like this to get the stable version
git clone -b stable https://github.com/TryGhost/Ghost.git ghost
and then
cd ghost/
git submodule update --init
npm install -g grunt-cli
npm install
grunt init
The error I am getting is that is not able to find grunt
Ghost git:(stable) grunt init
zsh: command not found: grunt
You're not supposed to install grunt globally, only grunt-cli. The grunt package should be installed in your project directory using something like:
~$ npm install grunt --save-dev
That will add a line to your project's package.json file, then you configure a "Gruntfile.js" for your project with tasks that are run from the command line with:
~$ grunt taskname

Resources