NodeJS npm - Using Contextify on windows - node.js

I'm able to build contextify 0.1.1 using node-gyp on Windows, but I don't know how to get npm to recognize/use this version when resolving other modules' dependencies. 'npm install' fails with the same (expected) 'node-waf was unexpected at this time.' error, despite having a build version of contextify in the local directory.
Put simply: how do I tell npm to use a module I've built myself, instead of trying to download/build its own?

npm install installs modules into the current directory's node_modules directory. So instead of using npm install at all, just move/copy the contextify module that you built yourself to node_modules/contextify. Unfortunately you will have to do this for any module that depends on contextify.
Hope that helps!

Related

Error: Cannot find module 'ffi' compiling ElectronJS project

I'm new on NodeJS/ElectronJS.
I need to use User.dll functions.
My actual situation is:
Windows 10 on Parallels
Node -v = 10.15.3 (LTS)
NPM -v = 6.9.0
I installed:
npm install --global --production windows-build-tools
npm install win32-api
npm install ffi (gives me several "\ffi.cc(***): warning C4996: 'v8::Value::To Object': .... deprecated)
I added var FFI = require('node-ffi'); in my "main.js" and when I try to compile with npm start
I obtain this error:
Error: Cannot find module 'node-ffi' at Module._resolveFilename (internal/modules/cjs/loader.js:584:15)
What's wrong?
The following steps fixed my issue (major pain in the ***)
Make sure the node gyp compiler is installed
npm install -g node-gyp
Install the FFI package into the local project
npm install --save ffi
I also needed to install ref-array (part of example code)
npm install ref-array --save
Go into the node_modules/ffi directory and do an NPM install to make sure it's got all it's dependencies
cd node_modules/ffi
npm install
Get back out of the node_modules/ffi folder
cd ../../
Install the electron rebuild tools
npm install --save-dev electron-rebuild
Run the electron rebuild script (I'm running on Windows, hence .cmd)
.\node_modules\.bin\electron-rebuild.cmd
So simple (NOT) :D
Solved with this:
How do I resolve "Cannot find module" error using Node.js?
using npm install
and then
Node - was compiled against a different Node.js version using NODE_MODULE_VERSION 51
using
./node_modules/.bin/electron-rebuild

NPM Install is not installing dependencies

I'm attempting to install the Ushahidi V3 Client. I've been following the install process up until when I need to build the project from the source repo using npm and gulp - both of which I've had zero experience with. Whenever I run sudo npm install in the project directory, the process runs without complaints. However, when I run npm ls to verify that dependencies have been downloaded, I get a bunch of dependencies listed out as being missing.
How do I get npm to resolve all of these dependencies?
System Details
OS Ubuntu 14.04 (Trusty)
Node JS v0.12.9
NPM v3.5.1
What I've tried
Removing node_modules folder and re-running sudo npm install as referenced in this SO answer for a similar question: npm Gulp dependencies missing, even after running npm install
Uninstalling and reinstalling node and npm
#Strainy, as your research :D
It was a combination of running as sudo and not having the build-essentials.
That's why you should not use sudo npm
Follow these steps:
try npm uninstall. and then try npm install.
Also If it still doesn't work.
Try:
npm install -g npm-install-missing
or
npm-install-missing
For further reading, click here.

warn uninstall not installed for npm

I downloaded and installed nodejs on Windows. I'm trying to uninstall the version of typescript I have to get to an older version. When I run
npm uninstall typescript
or
npm uninstall -g typescript
I get
npm warn uninstall not installed in C:\Program Files\nodejs\node_modules: "typescript#0.9.7"
How can I uninstall typescript without having npm uninstall work? Thanks in advance.
You should have been able to uninstall typescript with the command
npm rm typescript
npm rm -g typescript
which is equivalent to uninstall. If that doesn't work, it is safe to find the typescript directory in Windows Explorer under C:\Program Files\nodejs\node_modules and remove it.
This should get you to a state with no typescript installed:
npm ls typescript
npm ls -g typescript
both show (empty).
Second point: npm view typescript version is not showing you information about your local setup, but rather the state of the npm repository, so it will always show you the version that corresponds to typescript#latest. At the time you asked this question, it was 0.9.7; it is now 1.3.0
Finally: to install a specific version of typescript such as 0.9.5, do
npm install -g typescript#0.9.5
Also, in general, you should make sure you have the latest node and npm. Updating npm on windows is a little tricky; you should follow the guide here: https://github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows
I recommend you to reinstall npm as this shouldn't happen. When the issue is still going on, open the directory and manually remove the files from that package.

How to use a specific version of NPM?

How can I switch which version of npm I'm using?
Currently:
$ npm -v
1.1.65
But I need: 1.0.x
I tried but got an error:
$npm version 1.0
npm ERR! version No package.json found
Anyone know how to use a different version of NPM? Thanks
Your NPM version is tied to your NodeJS version. As far as I can tell you can only have one NPM version per Node version. Using something like nodenv or, my favorite, asdf, you can define your node version per folder.
Per Node version (e.g. per folder) you globally install the version of NPM that you want to use.
$ npm install -g npm#x.x
Edit: You can also now specify NPM version in your package.json "engines" key, though it takes a little more work to enforce that declaration.
You can update npm without installing another version of node.js and npm is not tied to versions of node.js specifically.
Of course, as node.js advances and adds features so too does npm so there is some limit for npm depending on node.js but you SHOULD update npm as much as you can because there are important security patches and bug fixes.
This is a best practice for all software. Stability is not worth ignoring a security risk.
The command is npm install npm#latest -g to install it globally. This will install the latest version that will run with the node.js you have installed.
Additionally you can install a specific version of npm to your package.json in a project like this npm install npm#6.14 and you can use it locally. What is even more interesting is you can install a local version of node.js in a project and use it too!
Example:
I am running node 16.8.0 and I have npm 7.21.0 but I want to use an earlier version of npm but just for a project:
In the project directory
npm i --save-dev npm#6.14
This will put the earlier version of npm into the node_modules/.bin which will let you run it in relation to this project. If you type npm -v at this point you will get your global version.
If you type node_modules/.bin/npm -v you will get 6.14.15
Since you have made this version of npm the one for this project, when you use npm in your scripts it will use 6.14.15 and not the global version.
The same is true of node.js.
You can install an older version of node.js like this: node_modules/.bin/npm install --save-dev node#lts which will then add this version of node.js to the project.
You can test this by entering node_modules/.bin/node -v and in my example you will see the locally installed version number, in my example v14.17.3 although my global is v16.8.0
If you do this, your project scripts in package.json scripts will run the locally installed versions rather than the global versions.
You can test this out by creating a script in your package.json.scripts like this: "what:version": "npm -v && node -v",
Then if you run npm run what:version you will get
> what:version
> npm -v && node -v
6.14.15
v14.17.3
What value is this?
It is a way for you to package node and npm with your project. This can be especially valuable for large projects where you have many people working on the same project, so you have consistency across the board or if you have a build server that has a specific version of node + npm. Additionally if you have corporate requirements, etc.
Additionally there is npx which allows you to do so much more. Using npx you can even try something out without installing it.
Here is the documentation for npm: https://docs.npmjs.com/about-npm-versions
If you find you are needing to switch between different versions of node.js and npm you might want to use nvm https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
When you install npm you only get the latest package, so you need to install specific versions individually, e.g:
npm install -g npm#5.6.0
Once you've done that, run a version check and you should see the version you've just installed:
npm -v
5.6.0
You can install specific version by bellow command.
npm install -g npm#6.14.11
For a specific version, use
npm install -g npm#x.y.z
And for the latest version, use
npm install -g npm#latest

How to override a npm install version check failure?

Is there a way to override npm's version checking against a packages.json file from command line (npm install command)?
I would like to try and install/run a package even though it doesn't match my node.js version.
This is the error I am getting:
npm ERR! Unsupported
npm ERR! Not compatible with your version of node/npm: canvas#0.6.0
npm ERR! Required: {"node":"0.4.x"}
npm ERR! Actual: {"npm":"1.0.8","node":"v0.5.0-pre"}
Thanks
What you could do is go to github package and update the package.json. Then just install it locally. But most of the times these version numbers are there for a reason. The node.js api changes sometimes(I believe 0.5.0-pre changed, because socket.io also fails with 0.5.0-pre).
If you for example install nvm, nave you can keep several version of node.js side-by-side easily.
If all else fails you could still manually download and copy the files in your node_modules folder

Resources