How do I use lodash, and JS modules in general - javascript

I just installed Node and I want to use the lodash library. I made a new JS file in my desktop and I started the file with
var _ = require('lodash');
However, I get
Error: Cannot find module 'lodash'
How do I use the lodash library? All I can find online says to use the import statement I already used.

You need to install lodash with npm:
npm install lodash
That will install lodash into a subdirectory of your current directory called node_modules. Node.js knows to look there when you use require('lodash').
Usually you'll have a package.json file that tracks your dependencies so that you can just do npm install to install all dependencies for a project. If you want to add lodash to package.json then you can use npm install --save lodash.
If you don't have a package.json for your project yet, I would recommend running npm init to create one.

Related

How do you add Node to the frontend?

I have been coding with a React frontend and a Node/Express backend. But sometimes, I only need some plain Javascript without React, but still want the benefit of NPM and other Node modules. What is a way to do this?
You'll need a module bundler of some kind. There are many options including Webpack, Browserify, Gulp, and Parcel.
For Webpack, for example, from their example docs, the process could be:
Install webpack with npm install webpack and install webpack-cli
Install a module you want to use on the frontend, eg lodash
In src/index.js, import lodash: import _ from 'lodash'; and use it as needed. (You can also import other modules from NPM or from other places in your source code)
Set up webpack.config.js if you need custom build configuration settings
Run webpack to build the project: npx webpack. A single bundled JavaScript file will be created which contains all your source code and the imported Lodash's source code.

How do I see what requires a package using yarn?

My node app has a dependency that depends on node-webcrypto-ossl.
I'd like to run something like:
yarn what-depends node-webcrypto-ossl
To see the dependency chain, eg:
A
B
node-webcrypto-ossl
However I can't find anything to do this in the yarn CLI docs.
How do I see what requires a package using yarn?
yarn added the yarn why command specifically to get information on why a package is imported.
For example:
yarn why node-webcrypto-ossl

Import elasticsearch module in Ember.js

I'm trying to import a browser build for the elasticsearch.js client. I'm using npm as my package manager since EmberJS (v2.13.1) is moving away from bower:
npm install elasticsearch-browser
Few questions:
Can I directly import the module as it is "built for the browser" and probably doesn't use any CommonJS syntax? If yes, what would the import statement look like?
Am I required to use browserify to import any module I install from the npm registry? Why? How do I know which packages are browser-ready?
I could install the module through bower and then do an app.import in the ember-cli-build.js file. Would that work?
As I understand, it finally comes down to using bower vs installing browserify, correct? But I still don't understand why I should have to use a transpiler.

Check which npm package installs lodash dependency

I am using webpack to build a production bundle for an application, and lodash, although I have not explicitly installed it and it does not exist in my package.json, is being inserted in my node_modules folder.
When I run npm uninstall --save-dev lodash (which effectively removes the lodash folder from node_modules), and rerun the build process, my bundle shrinks significantly. I would like to determine which package includes and requires the full lodash library.
How can I determine which of my many npm packages are requiring lodash?
Note:
I am using the packages lodash.debounce and lodash.throttle. But lodash.throttle only has a dependency of lodash.debounce and lodash.debounce does not have any other lodash dependencies.
I was looking for typescript package which is upgrading from 3.5.3 to 3.9.7 implicitly.
With the help of #Kenan I will make contribution:
When run as ll or la, it shows extended information by default.
Blockquote
https://docs.npmjs.com/cli/ls.html
npm [ls|la|list] [[<#scope>/]<pkg> ...]
In my situation typescript version ^3.5.3 in package.json but it was upgrading to 3.9.7 because of ^ character. npm is intalling this package's latest version of major version 3. Installed version 3.9.7 doesn't match in any typescript dependency in package.json files.

Where is lodash.js after install via npm?

I ran npm insall lodash but I can't find the lodash.js file to include in the browser. I realize I can just download lodash.js but I would prefer to use npm if possible.
Newer versions of lodash packages replace lodash.js with index.js. Use node_modules/lodash/index.js.
npm installs all of the modules in to the node_modules folder. So you'd find lodash in ./node_modules/lodash/
If you're using browserify, you don't need to worry about that location as you can simply var _ = require('lodash'); and browserify will take care of the files for you.
More info on Browserify at: http://browserify.org/

Categories

Resources