Installing npm packages, after recieving the node modules folder - javascript

My name is Jaffer Syed and I am wondering how to install npm packages into my code.
I am trying to install num2fraction https://www.npmjs.com/package/num2fraction but in you npm package video you didnt go into how to install npm packages and how to actually make it work in the javascript. I added the command for the package in the command line but it doesn't seem to work for some reason and when I find the error it keeps saying that the require is not defined.
my js looks like this for trying to implement the num2fraction
var n2f = require('num2fraction')
console.log(n2f(2.555))
thank you

here is a sample script for how I did what you're trying:
mkdir foo
cd foo
npm init
just accept all the default options here
npm install -s num2fraction
put your exact contents above into a file called foo.js in this directory and save
node foo.js
should output 511/200

Related

'vue' is not recognized as an internal or external command

everything was installed correctly. but whenever I try to create project, it says "'vue' is not recognized as an internal or external command". I installed and re-installed but didn't work. npm was also added to environmental variable path.
C:\Users\touha\Desktop>npm list -g --depth=0
C:\Users\touha\.npm-packages
`-- #vue/cli#3.8.2
C:\Users\touha\Desktop>vue ui
'vue' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\touha\Desktop>
Locate vue.cmd and add its location to your PATH
It is added to package manager(npm or yarn) installation. So you may find it at following locations
YARN
C:\Users{YourAccount}\AppData\Local\Yarn\bin
NPM
C:\Users{YourAccount}\AppData\Roaming\npm
just open PowerShell and run: npm install -g #vue/cli
Addition: If setting the path variable and reinstalling #vue/cli does not work, using the node.js command prompt instead might solve the issue.
It seems vue isn't been installed globally for some reasons.
This is the step I followed to solve mine:
Firstly, create your desired project folder (say "Vue Project"). This is where you want to create a vue project.
Then create a "node_modules" folder in the Vue Project folder
Then go to your system npm folder C:\Users{YourAccount}\AppData\Roaming\npm
You will see three different "vue" files. Copy them and paste in the Vue Project Folder you created.
Go back to C:\Users{YourAccount}\AppData\Roaming\npm and enter the node_modules folder. You'll see a "#vue" folder. Copy this, and paste it in the node_modules folder you created in the Vue Project Folder.
You can now head back to the CLI and create your project using "vue create my-vue-project" where my-vue-project is your desired vue project name.
You can try this way it worked for me
go to the location of your yarn or npm mine is C:\Users\TED\AppData\Local\Yarn\bin for Yarn
C:\Users\TED\AppData\Local\Npm\bin for Npm users
TED will be replaced by your user name
then copy and add it to your system environment variable
Note in case you don't find AppData make sure you have view hidden file checked
I am using Yarn to install #vue/cli.
The way I solve it is via the following steps
Locate your global installed vue.cmd location
Add the vue.cmd directory into System variable Env. For myself, the path is C:\Users{MyAccount}\AppData\Local\Yarn\.bin
what worked for me:
In a powershell :
npm install vue
npm install -g #vue/cli
C:\Users\{USER}\AppData\Roaming\npm\vue.cmd create {NAME}
if you got this error most probably chance package not installed completely
check-in C:\Users\dev\AppData\Roaming\npm
if you had not found the package under this folder then re-run your command
Run command prompt as administrator
Run setx /M path "%path%;%appdata%\npm
Restart the command prompt
Now create the Vue project

how do I install an npm package (math-simple-integrals)

Hello I have been trying to install the npm package 'math-simple-integral'
and I am having trouble making it work, I originally used math.js and just used the cdnjs script src
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.0/math.min.js"></script>
and that worked for all of the functions that I needed but it didnt work for the math.integral() function that I am looking for I am not sure how to install 'npm install mathjs-simple-integral' and I am wondering if there is a script src that I can use like I did for the math.js.
any assistance would be appreciated.
thank you
In order to run npm install, you must first install NodeJS.
You then run npm install mathjs-simple-integral from the command line at the root folder of your project. This will create a node_modules folder which contains the JavaScript files you want to link to.
Then you would reference it with something like:
<script src="node_modules/mathjs-simple-integral/index.js"></script>
However, you'd probably want to use something like Gulp, Bower or WebPack to include your dependencies.
Then you can simply reference it by importing it to math:
math.import(require('mathjs-simple-integral'));

Cannot find module '../build/Release/opencv4nodejs

I am new to OpenCV and node js, trying to learn OpenCV in node js.
I came across opencv4nodejs which I found best for learning.
I have followed all steps to install OpenCV and setting environment variables for OpenCV and done npm install opencv4nodejs also. able to run Quick start code given but when I try to run example machineLearningOCR.js from examples folder I get error Error: Cannot find module '../build/Release/opencv4nodejs'
I run command node machineLearningOCR.js from cmd but getting error mentioned above on line const cv = require('../');
How to run examples given in opencv4nodejs?
If you cloned the repository and want to use the examples directly from the examples folder then you have to npm install in the root directory first to build the module.
Optionally you can npm install opencv4nodejs in any project directory, copy the examples into that directory and replace const cv = require('../'); with const cv = require('opencv4nodejs');.
You can also directly submit an issue here: https://github.com/justadudewhohacks/opencv4nodejs/issues
I had the same problem and I did solve the issue with the following steps.
First all of. I checked the npm version, for this case I recommend you updated or pass your software resources at the next version.
NodeJs 14.16.0
NodeJs version: 14.16.0. You can verify the version with this command if you had another NodeJs version installed
node --version
Npm version: 6.4.11 You need verify if you had this version installed only you will able to validate this if you have NodeJs but if you didn't have node you can install the version I recommend you
npm --version
After you set the NodeJs and npm versions you need to verify the following things
Have visual studio 2015 or latest with the development for desktop component
Have cmake program installed
Have git bash
Have Java or the language you will to work
Have the environment variables for the above programs I mentioned
Then you can try again to install open Cv
You need to reopen another or new terminal and then you will execute this command
npm install --save openc4nodejs
And then
npm install -g openc4nodejs
After than or before don't forget to install appium I recommend you 1.19.1 version
You can verify if you have appium with opencv with open Cv doctor you can run appium-doctor and check if the Open cv component are present.
For to install appium doctor you can run this command, npm install -g appium-doctor
I hope this answers will be helpful for you
Autor: Eder Carbonero

How to make a nodejs script be a CLI tool in linux?

I made a javascript file mytool.js, it has some dependencies (in package.json).
Now I can execute typing in
~/mynodes/mytool $ node mytool
But if I change the working directory I can't use this command anymore because previously it was run locally.
What I want to achieve is to be able to just type :
~$ mytool
(wherever I am in my system's filesystem and without typing node before).
Should I install it manually ?
If yes, where is the common location to install a personal nodejs script in a unix-like system ?
Or is there a npm-like command to install a personal script system
wide ?
When you add a "bin" key in your package.json:
"bin": {
"mytool": "mytool.js"
},
then you will be able to install your script with npm install -g and it will be automatically added where it should be (to a place where other globally installed cli tools are installed, which should be in your PATH).
You can see this simple project as an example:
https://github.com/rsp/node-websocket-vs-socket.io
It was created as an example for this answer but it does what you need:
it has a single script to run
it has external dependencies
it can be installed globally
it can be run from any place with a single command
Note that you don't need to publish your script to npm to be able to install it - though you can do it, or you can also install projects directly from GitHub (including private repos) - but you can also install a module that you have in your local directory or a tarball:
npm install -g module-on-npm
npm install -g user/repo-on-github
npm install -g /your/local/directory
npm install -g /your/local/tarball.tgz
For more options, see:
https://docs.npmjs.com/cli/install
Also keep in mind that for your program to be able to be executed from anywhere, you need to use paths relative to __dirname or __filename if you need to access your own files relative to your code. See:
https://nodejs.org/api/globals.html#globals_dirname
Put a shebang line at the top of the script (e.g. #!/usr/bin/env node).
Put the script in a directory in your $PATH
Give it executable permission (e.g. chmod +x /usr/local/bin/example.js)
First option:
You can run your file globally by putting on the first line of the file : #!/usr/bin/env node, copying it to /usr/local/bin and make it executable: sudo chmod +x /usr/local/bin/yourfile.js and then you can call it from where you want with yourfile.js
Second option:
Make your local file executable and create an executable bash script which calls your local file and put it in /usr/local/bin and then call the bashfile globally.

Yarn global command not working

I'm working with Yarn v0.16.1. If I understand correctly (according to the documentation), yarn global add <package> should be the equivalent of npm install -g <package>. However, when I run the example in the docs (with create-react-app), the command runs successfully but create-react-app is then not available from the command line. Here's the output:
$ yarn global add create-react-app
$ yarn global v0.16.1
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 📃 Building fresh packages...
success Installed create-react-app#0.6.0 with binaries:
- create-react-app
✨ Done in 3.22s.
$ create-react-app --help
-bash: create-react-app: command not found
Doing a global install with npm has the expected result, and everything works. What am I missing with yarn?
You should add export PATH="$PATH:$(yarn global bin)" to your ~/.bash_profile or whatever you use. It would solve the issue.
Depending on how you installed it, Yarn's global folder varies for some reason. You can follow this issue here.
Update Dec 2018
Just updating the path didn't work for me. I had to also set the yarn prefix.
Ubuntu 18.04.1 LTS (bionic)
yarn 1.12.3
npm v3.5.2
node v8.10.0
zsh 5.4.2
Steps
Confirm your global bin path
yarn global bin
I got: /home/username/.yarn/bin
set yarn prefix:
make sure your yarn prefix is the parent directory of your bin directory. You can confirm by running
yarn config get prefix
when I ran this, my prefix was empty, so I set it:
yarn config set prefix ~/.yarn
add the following to ~/.bash_profile or ~/.bashrc
export PATH="$PATH:`yarn global bin`"
for zsh users, be sure to add this line to ~/.zshrc
restart your shell or start a new one
bash -l or zsh
In my case yarn global bin wasn't working.
Try a brew reinstall yarn if that's how you installed yarn
👍🏻
Ran into this issue but on a Windows system. All I had to do was add the yarn global bin to the PATH variable.
setx path "%path%;c:\users\YOURUSERNAME\appdata\local\yarn\bin"
Remember to change YOURUSERNAME to your account username.
You can check where your global bin is by running
yarn global bin
And please don't forget to restart the CMD window that you're working on.
Happy coding!
! WARNING !
When executing the command that %path%; in front of your yarn directory is very important. If you don't type it you will definitely replace all your system environment variables.
Option 2
Safer option would be would be to just go to System Properties > Advanced > Environment Variables > select PATH the click Edit > then append and not replace with ;c:\users\YOURUSERNAME\appdata\local\yarn\bin
For macOS, you just need one step,
export PATH=~/.yarn/bin:$PATH
To reinstall run below.
brew install -g yarn
// Note:(updating homebrew) for Mac users.
brew reinstall yarn
// (if yarn is still not found)
Here's the process I used on Mac OSX El Capitan.
Try to install yarn (I used the Curl command) if you have it already. This will bring up if its already installed and will give you the current directory where it exists.
Like this:
curl -o- -L https://yarnpkg.com/install.sh | bash
It will display something like this:
Installing Yarn!
> /Users/{Your Username}/.yarn already exists, possibly from a past Yarn install.
> Remove it (rm -rf /Users/{Your Username}/.yarn) and run this script again.
Do not remove it. Move on to step 2.
Copy the directory listed above. Open your profile. I'm using zsh. So mine was ~/.zshrc. You can find yours and more info here.
Copy the following (replacing your directory and username details as necessary, the one you got from the installation error message).
alias yarn="/Users/{Your Username}/.yarn/bin/yarn"
Try to run yarn version to check if its working. If it is, you should see a version number displayed in your terminal.
That's it, and what worked for me.
AndrewD gave a great answer, but for those on Windows it's a bit different, especially step 3.
Please do not use the command setx as #Thapedict said.
It will remove all your previous paths you had and replace it with only the given directory.
If you want to use setx, I think there might be a flag to make it append the existing path variables, but I don't know which one. Using only setx will not append to it.
I would just do System Properties > Advanced > Environment Variables > Edit Path > New > c:\users\YOURUSERNAME\appdata\local\yarn\bin
to make sure you don't remove your previous variables.
export PATH="$PATH:$(yarn global bin)"
On Ubuntu, I fixed by installing the package like this:
sudo yarn global add <package> --prefix /usr/local
When switching from bash to zsh, I got the same error, it worked for me:
in your .zshrc
# NVM Stuff
export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"
# YVM Stuff
export YVM_DIR="$HOME/.yvm"
. "$(brew --prefix yvm)/yvm.sh"
As I understand it, my mistake arose due to the lack of access to version managers
I faced a similar issue on Windows after installing Yarn then Vue Cli.
Packages were installed but not accessible.
The problem was that the directory where yarn packages are installed is not in PATH.
The default installation directory for Yarn packages in my case was
C:\Users\USERNAME\AppData\Local\Yarn
So make sure to add this directory to path.
Putting this answer so that hopefully it can come in google and help others.
expo command not found after using
yarn global add expo-cli don't work in ubuntu 20.04
It's no longer necessary to modify your path for yarn
Newer versions of yarn include yarn create and yarn exec
In the case of create-react-app you can run yarn create react-app see this blog post https://classic.yarnpkg.com/blog/2017/05/12/introducing-yarn/
To run a non-creating package like serve, you can still run it without modifying your path! After installing it with yarn global add serve try this: yarn exec serve

Categories

Resources