1

I am new to nodejs packages and what I understood was that to share code you had to do a module.export (in addition to adding a package.json)

For example bootstrap-select does not have an export function but is available on npm.

So my question is do all modules require an export and also can I do a require('bootstrap-select') in my code?

JD.
  • 15,171
  • 21
  • 86
  • 159
  • It looks like that is available via NPM for package management but is intended for use in a browser and not using Nodes module loading pattern. I suspect it wouldn't work in node how you hope. You could always write a wrapper for it. http://stackoverflow.com/questions/5171213/load-vanilla-javascript-libraries-into-node-js – lemieuxster Apr 29 '15 at 03:49

3 Answers3

3

no, all npm modules do not require an export. npm is now being used more generally for not only javascript packages intended for use under node.js but front end code for browsers, CSS libraries, etc. At the very least, an npm package could just deliver a payload of files not even including any javascript, such as some images, some CSS, some HTML, etc.

So you can only do require('some-module') if that package has either an index.js file or has set the main property in it's package.json file properly.

However if you are authoring a javascript module for node.js, then yes you'll need to export something in order for your module to load properly.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
1

No, npm modules do not require doing something with module.exports. If you do not touch that object, requireing your module will return an empty object (since that is the default for module.exports. However, this can be useful if your module is only intended to be required for side effects, rather than a return value.

For example, the module you linked to modifies global state by attaching a jQuery event handler.

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69
0
As per i know ,
1.All npm modules are not required to build an app.
2.If we use var bootStrap = require('bootstrap-select'); using bootStrap variable you can access bootStrap module.
so we can pass that object in anywhere of your code
3.To install a dependency modules,
In package.json give dependency block as like this
"dependencies": {
"express": "2.3.12",
"jade":   "latest",
"redis":   "0.6.0"
}
you can change and edit your packages. then enter a command npm install in command prompt it will install only dependency modules.
If i made any mistakes please correct me Thanks.
nirmal
  • 51
  • 5