NodeJS installed modules are not recognized on Windows 10 - node.js

I need some help to setup NodeJS on a Windows 10 machine.
I installed the 10.15.3 LTS version from the official website which has done the following:
Node installed at C:\Program Files\nodejs\
Added to the Path in the user's variables: C:\Users\rsantos\AppData\Roaming\npm
Added to the Path in the environment variables: C:\Program Files\nodejs\
Created an empty npm folder at: C:\Users\rsantos\AppData\Roaming
Then with npm init I initialized the package file which was created at C:\Users\rsantos.
Followed by npm install supervisor which:
Created a npm-cache folder at C:\Users\rsantos\AppData\Roaming\npm-cache
Created a node_modules folder (with the supervisor) at C:\Users\rsantos\node_modules
Created a package-lock.json file at C:\Users\rsantos with:
{
"name": "rsantos",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"supervisor": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/supervisor/-/supervisor-0.12.0.tgz",
"integrity": "sha1-3n5jNwFbKRhRwQ81OMSn8EkX7ME="
}
}
}
Updated the package.json file to:
{
"name": "rsantos",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"supervisor": "^0.12.0"
}
}
Then when I open the command line and type:
C:\Users\rsantos\Desktop>supervisor --watch C:\NodeJS_Projects\Proj1\ -e js C:\NodeJS_Projects\Proj1\Test.js
I get:
'supervisor' is not recognized as an internal or external command, operable program or batch file.
After that I tried to install supervisor globally and it started to work.
The next error was 'require' not being recognized after installed locally.
I followed the same approach which made supervisor work and installed require globally. My code is requiring the module with require('request'). But even installed globally, it fails with this error:
Error: Cannot find module 'request' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
Notes:
I uninstalled everything and reinstalled, the result was always the same.
Also played with the environment variables but still no good.
npm list -g request outputs this: C:\Users\rsantos\AppData\Roaming\npm -- request#2.88.0
At this point my C:\Users\rsantos\node_modules folder has all the modules, including supervisor and request.
The package.json has supervisor and request as dependencies.
The package-lock.json seems to have all the modules in the node_modules folder, including supervisor and request.
I still get the Cannot find module 'request' error.
Can someone help please?

when you want to use a NPM package as tool (e.g. >supervisor --watch PATH), you can install the package --global. When you want to load the package in Node with require() I would install it locally.
Does the following work for you?
Install the tooling globally:
npm install --global supervisor
Set up your project in local folder:
mkdir project
cd project
npm init -y
npm install --save request # install to project/node_modules
node index.js # start your app

Related

Managing Node JS dependencies

I am learning node js and I have put together a small website which is using a few dependencies like jquery and a couple of other js files. When it comes to moving the site to a live server how do I migrate the dependencies that I used in production? Would I have to download the jquery files and other js library files then add the link into my script tags like you normally would as I guess everything in production points at the node_modules folder?
Sorry for sounding a little daft but I’m still learning my way with npm.
Appreciate any advice
Rufus
In Node.js, server side dependencies are saved in the package.json file and these describe any dependencies you'll need.
You create this file by typing
npm init
This will prompt you for some details (which you can skip with -y flag.)
To install your server side dependencies, I presume you've used:
npm install <dependency>
To ensure this is saved you can type
npm install <dependency> --save
e.g.
npm install lodash --save
This will update your package.json.
package.json should be stored in source control and your dependencies can be restored using:
npm install.
An example of a package.json file would look like this:
{
"name": "node_test_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.11"
}
}
If you see a package-lock.json file (later versions of NPM), this should be added to source control too.
Personally I just use npm install lodash
Then
app.use('/scripts/lodash', express.static(__dirname + '/node_modules/lodash/'));
that way I can use this as (in node.js)
const _ = require('lodash');
or in an html file as
<script src='/scripts/lodash/lodash.js'></script>
Preventing the whole "copying to public/js"...
Or if you're just talking about a build process, you just run npm install on your productions server and it installs all the packages from packages.json again.

Cannot install vue

I usually perform fresh re-installs of packages after a period of non-use, but when I came to perform a fresh install of Vue.js and vue-cli I tried to perform a vue init on a new project but was told that vue was not recognised as an operable or batch file etc.
It is possible that I created a problem when I uninstalled vue-cli, which removed something like 250 packages. Subsequent reinstall only installed 1 package, but I was then unable to get any response from the vue init command other than the above.
I have tried every workaround I could find on SO and elsewhere which boils down to the following: -
Upgrading node and npm
Upgrading git
Cleaning the npm cache
One consistent error message in my attempts to perform npm install vue was that I lacked a package.json file in my C:/Users/user folder (I am said user and I have administrator privileges). I ran npm init to create a blank package.json file in that folder but that didn't work either.
The root problem seems to be that nothing I do re-installs a vue.cmd file in my AppData/Roaming/npm folder, even though this path is properly installed in my environment variables. I have looked if this file has been installed elsewhere on my machine, but it hasn't.
Is it possible to manually download a vue.cmd file and put it exactly where it needs to sit, i.e. in my AppData/Roaming/npm folder? I googled this to no avail. All this is while running Command Prompt as an administrator.
Is anyone else having this problem? Is there something with another JS framework which is currently blocking Vue from being properly installed? (I have also developed projects in React and Angular5.)
Just as an update, this is the package.json file I currently have in my C:/Users/user folder after running npm init from that folder: -
{
"name": "user",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"npm": "^5.8.0",
"vue": "^2.5.16"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
It sounds like you don't have vue-cli installed on your machine
But without any error messages it's impossible to tell.
try npm install -g vue-cli#2.9.3
then run which vue-cli to check the location

node local dependency installs as shortcut and nested instead of flat

It seems this started when I updated node/npm but I didn't realized until now when I had to delete and re create my node_modules folder.
I have a React Native project which has the core module and one Examples project to showcase the module. The examples project references the module like this in my package.json:
"dependencies": {
"module-core": "file:../core"
},
When I run npm install in the Examples project I was getting this nodule_module structure:
node_modules
core
core_dependency_1
core_dependency_2
Now, I get this:
node_modules
core
node_modules
core_dependency_1
core_dependency_2
At first I thought it had to do with peerDepencies and how npm handled flat/nested dependencies but I have checked and it seems now the core folder is now a shortcut (I am using Windows).
This is breaking my gradle scripts because some are referenced like this:
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
I could fix this by renaming the links but that would make the build platform/environment dependent.
Also it breaks some other scripts like this one:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
That is because the project() resolves to my root folder and now I cannot use that either.
I am using npm 5.4.2 and node 8.8.1. Previously I had node 7.4.0.
Is there any flag or way to make npm install the localDependency and not treat it as a shortcut?
I finally found the answer. Npm Version 5 indeed changed the way the local dependencies are handled and it just makes npm link, which creates symbolic links or shortcuts in windows.
You can accomplish the same behavior as before with this:
npm install $(npm pack <folder> | tail -1)
Working for me in Windows 10 with git-bash
My final solution was having this package.json in the Example project that used the core:
{
"name": "core-examples",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"preinstall": "npm pack ../Core | tail -1",
},
"dependencies": {
"core": "file:core-0.0.0.tgz"
},
"jest": {
"preset": "react-native"
}
}
The preinstall script will generate the tgz file and then you can install as usual. This will avoid having to commit the tgz file to the repository.

How to run `npm install && bower install` before `sbt compile` for Heroku Deployment?

I am working on a Playframework project which has front-end codes in sub-directory ./ui and managed by Grunt using https://github.com/tuplejump/play-yeoman
Currently I used https://github.com/heroku/heroku-buildpack-multi and set
https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-scala.git
in the .buildpacks file.
And set
{
"name": "scala-grunt",
"dependencies": {
"grunt-cli": "0.1.13"
},
"devDependencies": {
"grunt": "~0.4.5"
},
"version": "0.1.0",
"engines": {
"node": "~0.10.21"
}
}
in the package.json file of root directory.
However, when I pushed the code base to heroku it will throw an exception Fatal error: Unable to find local grunt. I think that is because sbt doesn't to run npm install && bower install in the ./ui directory.
Does anyone have ideas about how to run a command npm install && bower install before sbt compile in heroku?
Check out https://docs.npmjs.com/misc/scripts. There are a couple keywords you can use to run bower and grunt at different times. Check out the preinstall and postinstall keyword.
For an example, here is my script section from my package.json file that I use a lot.
"scripts": {
"start": "node lib/app.js",
"postinstall": "bower install --allow-root",
"test": "grunt"
}
This command solved for me:
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git

npm install doesn't install any dependencies

I am trying to install packages in the package.json file. Unfortunately, when I run npm install, nothing happens (nothing is installed). I have used npm install on other repos and it works successfully.
Here is my path:
$PATH = /Users/me/.rbenv/shims:/Users/me/.rbenv/bin:/usr/local/share/npm/bin:/usr/local/bin:/Applications/Postgres.app/Contents/MacOS/bin:/usr/bin:/bin:/usr/sbin:/sbin
As you can see, npm/bin is in my bath and I believe this is correct.
Here are the instructions for this repo and where to run npm install (which I am doing)
go into "module"
run "npm install"
pair your laptop/pc with intelligent brick troubleshoot: http://www.ev-3.net/en/archives/97
run example.js: "node example.js"
see "example.js" for more details
When I run npm install in the module directory (that has the package.json) nothing installs.
Here is the package.json:
{
"name": "ev3-nodejs-bt",
"description": "Bt Api for lego ev3 robot",
"version": "0.0.4",
"private": false,
"dependencies": {
"serialport": "1.*"
},
"main": "Ev3.js",
"devDependencies": {"serialport": "1.*"},
"scripts": {
"test": "node Ev3.js"
},
"repository": {
"type": "git",
"url": "https://github.com/wenchaojiang/Ev3NodeJsBtAPI"
},
"keywords": [
"node.js",
"ev3",
"lego",
"robot",
"bluetooth"
],
"author": "Wenchao Jiang <psxwj#nottingham.ac.uk> (http://wenchaojames.wordpress.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/wenchaojiang/Ev3NodeJsBtAPI/issues"
}
}
Do I have something set up wrong on my system? (I don't think I do based on my $PATH and successful installing packets in other node-js repos) Is this package.json file not valid?
npm install doesn't install (or echo) anything when all of the dependencies are satisfied. Ensure there's a serialport folder under node_modules.
If you want to reinstall everything, you just need to remove the node_modules folder:
rm -r node_modules
npm install
If you have an npm-shrinkwrap.json file, check it. The npm install command will only install the packages specified in that file.
According to the npm install docs:
If the package has a shrinkwrap file, the installation of dependencies will be driven by that.
I had the same problem with my project. And when I looked at my npm-shrinkwrap.json file, I saw dependencies: {}. So that's why it didn't install anything for me.

Resources