cannot found lodash in npm installation - javascript

I installed lodash and several other libraries this way:
npm install grunt-contrib-jshint --save-dev
npm install grunt-contrib-testem --save-dev
npm install sinon --save-dev
npm install -g phantomjs
npm install lodash --save
npm install jquery --save
When I run testem using grunt (or directly), I found the following problem
ReferenceError: _ is not defined
Digging into the issue, I found the issue in the mode following line
module.exports = require('./lodash');
(I have to point that the ./ is added by the installer. I even tried to remove it, but the error persist)
This is located on index.js, inside the node_modules/lodash folder
I think this is an issue of installation, but no matter what I do, it continue failing. I update npm, I remove node_modules and install all over again, but this continue happens.
Any advice or clue will be greatly appreciate

You don't need to pass the relative path of modules that are inside the node_modules directory, just passing the module name will do the trick:
require('lodash');
Now node.js will automatically look inside the node_modules directory for you, simple as that <3.

Related

Need help on a react.js error that I keep on getting

Every time I try running npm start on a new project I keep on getting this error. Does anyone know or have any idea how to fix this??
There might be a problem with the project dependency tree. It is
likely not a bug in Create React App, but something you need to fix
locally.
The react-scripts package provided by Create React App requires a
dependency:
"babel-jest": "^26.6.0"
Don't try to install it manually: your package manager does it
automatically. However, a different version of babel-jest was detected
higher up in the tree:
D:\node_modules\babel-jest (version: 24.9.0)
Manually installing incompatible versions is known to cause
hard-to-debug issues.
If you would prefer to ignore this check, add
SKIP_PREFLIGHT_CHECK=true to an .env file in your project. That will
permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact
order:
Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
Delete node_modules in your project folder.
Remove "babel-jest" from dependencies and/or devDependencies in the package.json file in your project folder.
Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem. If this has
not helped, there are a few other things you can try:
If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
Check if D:\node_modules\babel-jest is outside your project directory.
For example, you might have accidentally installed something in your home folder.
Try running npm ls babel-jest in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed babel-jest.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file
in your project. That would permanently disable this preflight check
in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-)
We hope you find them helpful!
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR!
react-portfolio#0.1.0 start: react-scripts start npm ERR! Exit
status 1 npm ERR! npm ERR! Failed at the react-portfolio#0.1.0 start
script. npm ERR! This is probably not a problem with npm. There is
likely additional logging output above.
npm ERR! A complete log of this run can be found in: npm ERR!
C:\Users\smaso\AppData\Roaming\npm-cache_logs\2021-01-26T22_50_48_484Z-debug.log
I also face this problem. Simple solution of this problem is:
1)create .env file.
2) add SKIP_PREFLIGHT_CHECK=true in the file.
3) npm start
If this fails to work, create a .env file in the root directory of your project and add the following line
SKIP_PREFLIGHT_CHECK=true
just go to your project's root directory and delete node_module folder and npm start your project.
Delete and redownload node modules and run application again
To fix the dependency tree, try following the steps below in the exact order:
Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
Delete node_modules in your project folder.
Remove "babel-jest" from dependencies and/or devDependencies in the package.json file in your project folder.
Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
Check if ./babel-jest is outside your project directory.
For example, you might have accidentally installed something in your home folder.
Try running npm ls babel-jest in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed babel-jest.
Seems like you created the react project using create-react-app and installed jest using the following command.
yarn add --dev jest babel-jest #babel/preset-env #babel/preset-react react-test-renderer
But in the documentation it says just to run the following command if you are using create-react-app
yarn add --dev react-test-renderer
[See the documentation][1]https://jestjs.io/docs/tutorial-react
This worked for me.
I used the following command to uninstall jest npm uninstall jest in the root project folder (not the client) and then used the react scripts start command npm start and it worked
removing jest.config.js and uninstall ts-jest solved my problem

Terminal package-lock.json

I am taking an online coding class and I just started learning my way through Terminal. I was trying to run a npm test but could not figure out why I was getting an error. I looked up different ways to solve it but it only made things worse as I have changed the package.json file into package-lock.json somehow.
Is there a way I could undo this?
Below is the command that I typed:
npm uninstall -g angular-cli
npm uninstall --save-dev angular-cli
npm install -g #angular/cli#latest
It would be greatly appreciated if someone could help me.
Thanks!
Not sure what you are trying to achieve here.
Each part of your command does the following
npm uninstall -g angular-cli
will uninstall the angular command line interface globally (this is what -g does).
npm uninstall --save-dev angular-cli
will uninstall the angular command line interface, and remove from devdependencies within your package.json file.
these two above are the same command apart from the extra arguments.
And then after that you are globally reinstalling the latest angular cli ??
Futhermore for future reference, in order to chain commands you need to add '&&' between the npm commands for one line
Hope that clears something up

Typescript command "tsc" undefined. Tried every known solution

Welcome,
I can't run Typescript all my day. I use Visual Studio Code. What I am doing is installing TS globally:
$ npm install -g typescript
Then when I check it:
$ tsc
I get that error in all cases:
bash: tsc: command not found
To that time I have tried following solutions:
Setting npm config:
$ npm config set prefix /usr/local
What I get after npm root -g is:
C:\Program Files\Git\usr\local\node_modules
Setting npm config by relative way to get (And I get it):
C:\usr\local\node_modules
Setting .bash_profile:
export PATH=/usr/local/share/npm/bin:$PATH
Setting .bashrc with PATH also...
Installing Typecript as dev dependency also don't work.
Other small solutions that I even don't remember.
Please for help. I can provide more information that you need - just say what.
Tried every known solution
Will definitely work:
Uninstall nodejs
Install nodejs (make sure you ask it to set PATH for you)
npm install typescript -g
🎊🌹

Cannot install my own npm package

I've been developing a later-to-be-release Open Source project with Node as a CLI tool. The CLI itself works great I only need to test if it works while on another project, so for that I installed the projects globally npm install -g without errors, but for the life of me I can't use the CLI.
I get the following error:
The odd thing is that the directory and file does exist in the global npm folder:
This is the project's package.json:
Am I not understanding how making a npm/node CLI works? What I'm missing?
EDIT 1:
This is my index.js file:
And this is the commander.js file:
EDIT 2:
After creating a test project as #AngYC suggested I could use the test cli successfully, while inspecting the difference I found this. Inside C:\Users\Ivan\AppData\Roaming\npm the .cmd of both projects are quite different:
EDIT 3 (Solution):
After fiddling around I found out that the file that really needed the shebang (#!/usr/bin/env node) was only index.js file and not the commander.js one. Removing the shebang in that file solved the problem
You may want to try to link your local package to your global executable list.
https://docs.npmjs.com/cli/link
All you have to do is run npm link in the folder you got your tool and it should make the command available globally.
Try to uninstall cli run npm rm -g cli or sudo npm rm -g cli. Then you run: npm install cli -g
If the problem persist, you might want to remove you npm package globally, probably there might be some conflicting things running.
Type this: %appdata% (either in explorer, run prompt, or start menu).
You can simply remove all globally installed npm packages by deleting the contents of:
C:\Users\username\AppData\Roaming\npm
Then you might also want to clear all the your cache run npm cache clear or npm cache clear --force as the case might be.
Then you reinstall all your packages that were install globally again.
If problem still persist, check this:
When you run npm root -g, it yields C:\Users\<username>\AppData\Roaming\npm\node_modules, or even, you should check your path maybe the executable binaries and .cmd files end up in C:\Users\<username>\AppData\Roaming\npm instead of C:\Users\<username>\AppData\Roaming\npm\node_modules, you will need to add that path to the PATH env. variable.
I hope this resolves your issue.

Local Npm module "grunt-template-jasmine-istanbul" not found

Here is my grunt config file - https://github.com/mdarif/JavaScript-Boilerplate/blob/1.3/GruntFile.js
It's not loading the module - grunt-template-jasmine-istanbul and getting Local Npm module "grunt-template-jasmine-istanbul" not found. Is it installed?
Loading the grunt tasks like require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
Hope someone can help quickly!
I have to install 'load-grunt-tasks' package to load multiple grunt tasks excludes grunt-template-jasmine-istanbul rather than 'matchdep' package then given code works like charm.
require('load-grunt-tasks')(grunt, {
pattern: ['grunt-*', '!grunt-template-jasmine-istanbul']
});
as i told you in your other post. just install that module:
$ npm install grunt-template-jasmine-istanbul
you probably should save it into your package.json, you can do that directly when installing the plugin:
$ npm install grunt-template-jasmine-istanbul --save-dev
edit: if you use ..('matchdep').filterDev, it filters the devDependencies in your package.json. grunt-template-jasmine-istanbul is missing in your package.json! easiest way to fix that is the second command i gave you above!

Categories