'var' is not defined as internal or external command in npm - javascript

I am trying to install histogramjs package. npm i histogramjs --->works well but when I try to run --> var hist = require('histogramjs')
I got the error on cmd:
'var' is not defined as internal or external command in npm
I have tried already tried
npm install var -- >get installed abd when I try to run ---> var env = require('var');
get the same error..
If run directly from node.js , get error 'cannot find module 'var'
Please help as I am very new to npm and node
Thanks

You should not type in var hist .... in your command line. What you type there, will be executed by shell, not by npm/node.
Instead, try:
Run npm init. Press enter couple of times, until it completes. This will create package.json file which is npm's configuration file. this file will tell npm, among others, which file is an entry point to your application.
Edit package.json, add "start": "node app.js", inside "scripts" element (see example below).
Create app.js in the same directory where package.json is located. Put your script in this file.
Run npm start.
If your script depends on 3rd party modules (in your case - "histogramjs" probably), install those as well:
npm install --save histogramjs
Running it without --save modifier will work as well. But --save will cause npm to put "histogramjs" as a dependency in package.json. Thanks to that, when someone gets your code later (i.e. from repository), he'll be able to simply run npm install, without even having to care which dependencies are required.
package.json
{
"name": "t",
"version": "0.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD"
}
app.js
var hist = require('histogramjs')
// ... rest of your code

Related

Error after cloning a Git in a different project NPM command

I have this problem that is not letting me debug my website.
You dont have a 'start' script defined in your "scripts" section.
You could use the defined 'dev' script (npm run dev) or rename it to 'start'.
"scripts": {
"start": "nodemon src/index.js"
}
Tipp: Use npm run to list all available npm sripts

How to make npx work with my custom npm pakage?

I've published a new package which aims to generate a very small boilerplate for a node open source project via cli. For now, it's just a combination of few npx commands and requires other npm packages like gitignore, license to work. I want to execute the build script in package.json with the following command.
npx get-set-node-oss build youremailaddress#xyz.com
Link to the npm package: get-set-node-oss. I know the name is a bit too long.
{
"name": "get-set-node-oss",
"version": "1.0.1",
"description": "One command setup for your Node OSS project",
"scripts": {
"build": "npx license mit > LICENSE && npx gitignore node && npx covgen"
},
"author": "Harshit Juneja",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/harshitjuneja/get-set-node-oss.git"
},
"keywords": [
"node",
"boilerplate","gitignore","MIT","OSS"
],
"bugs": {
"url": "https://github.com/harshitjuneja/get-set-node-oss/issues"
},
"homepage": "https://github.com/harshitjuneja/get-set-node-oss#readme"
}
I expect the user to make a new folder and cd into the folder and do
npx get-set-node-oss build emailstring
and get the resulting boilerplate files.
For npx to work you need to specify what the get-set-node-oss command means. Thankfully, this can be accomplished by using the bin field in your package.json file. More information from NPM documentation for your reference:
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
An example of it below.
"bin": {
"get-set-node-oss": //script you like to run
},
Hope it works out for you. I have understood bin as scripts in package.json. Where we can either do npm run foo or npx foo both yielding the same results. If you want an example, here you go https://github.com/vipulgupta2048/balenaclone

Module Not Found error after NPM Install

I'm new to Node and after successfully running NPM init for a node project, attempted to install lodash through:
npm install lodash --save
However, after running that command, I got the error:
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module 'internal/fs'
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/johnwolfe/.npm/_logs/2018-01-07T05_07_16_644Z-debug.log
I've also tried just running NPM install and I get the same error. What possible explanation is there for this error and how can I resolve it? I've tried other solutions on the web and nothing is working.
This is my package.json file:
{
"name": "notes",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Thank you for any insight!
Try the following ,
Delete all the dependencies of the project.
Reinstall.
rm -rf node_modules
npm install
I ended up having to do a deep uninstall of Node following this thread How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X) and then running Brew install Node again after resolving issues with symlinks.
I was trying to launch a React application using webpack.
I was getting module not found error even after installing it manually.error message even after installing the module
But "npm install" also gave below suggestion to run "npm audit fix"npm suggestion
I did "npm audit fix" as suggested.
It threw another error saying that existing version of another module does not meet the requirements of the latest version.
I had to install that version manually.
Then ran "npm audit fix" again and all the errors were resolved.
In my case, installation had issues as my intelliJ was not having enough permissions to create folders inside project workspace, following the below steps resolved my issue,
i) Ran my IntelliJ with administrator mode, but this was working for an instance after i restart i have to re-launch my IntelliJ again and this was an headache.
ii) Moved my workspace to path where IntelliJ has access to (earlier workspace was under users directory which was causing the permission issues).

How to run a script before installing any npm module?

I am trying to make a test for npm packages for my project such that every time I try to install a module i.e run npm install <module> a script must be run before that module is installed. The preinstall script only works with npm install and not with npm install <module>.
eg :- If run npm install request . It should run a script which shows me all the dependencies of the request module before installing the module. Thanks in advance.
Add "install": "[Your Command]" in the script part of your package.json
Example:
{
"name": "test",
"version": "1.0.0",
"description": "A sample test",
"main": "index.js",
"scripts": {
"install": "echo Test"
}
}
You can also use a pre hook with "preinstall": "[Your Command]"

Passing a param from cmd line to package.json

"scripts": {
"start": "gulp",
...
},
I'm using a package that runs via npm start. I want to pass params to the start command.
How can I pass the param in the command line?
npm start --myparam = hello
Alos, how can I access this in my package.jspn file to pass through to gulp:
"scripts": {
"start": "gulp --myparam",
...
},
If I understand your question correctly, you are trying to run gulp --myparam=hello with the npm start command.
The correct way to do this is npm start -- --myparam=hello.
Notice the -- in the command. Anything after the -- will be passed directly to the start command (gulp).
The reason for requiring the -- is that npm start (and npm run) can take parameters as well; like -s to silence the default output from npm.

Categories

Resources