Socket
Socket
Sign inDemoInstall

commonjs-walker

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

commonjs-walker

Analyzer and tree walker for commonjs.


Version published
Weekly downloads
8
decreased by-20%
Maintainers
1
Weekly downloads
 
Created
Source

commonjs-walker NPM version Build Status

Analyzes and walks down the dependencies from a commonjs entry and creates a walking tree.

var walker = require('commonjs-walker');

NOTICE that it will not walk down node_modules and any foreign packages.

walker(entry, [options,] callback)

walker('/path/to/entry.js', options, function(err, tree, nodes){
	// ...
});

If the file structure of your project is:

/path/to
       |--- index.js
       |--- a.js

index.js:

require('./a');

a.js:

// there's nothing.

Code:

walker('/path/to/index.js', function(err, tree, nodes){
	console.log(tree);
	console.log(nodes);
})

Then, the tree object will be something like:

{
	id: '/path/to/index.js',
	dependents: [],
	isEntryPoint: true,
	unsolvedDependencies: ['./a'],
	dependencies: [
		{
			id: '/path/to/a.js',
			dependents: [
				tree // points to `index.js`
			],
			dependencies: [],
			unsolvedDependencies: [],
			code: <Buffer>
		}
	],
	code: <Buffer>
}

The nodes object is the path->node hashmap.

{
	'/path/to/index.js': tree,
	'/path/to/a.js': tree.dependencies[0]
}

Walks down from a entry point, such as package.main of commonjs, and tries to create a walker.Module instance of the top level.

  • entry Path the absolute path of the entry point.
  • tree walker.Module tree of walker.Module
  • nodes Object the hashmap of <path>: <walker.Module>
options

All options are optional. By default, walker works in a very strict mode.

OptionTypeDefaultDescription
detectCircularBooleantruewhether should check circular dependencies
strictRequireBooleantruewhether should check the usage of method require()

Struct: walker.Module

Actually, there is no walker.Module exists. We only use it to declare and describe the structure of the module.

PropertyTypeDescription
idStringthe id of the module
isEntryPointBooleanwhether the current module is the entry point
dependentsArray.<walker.module>the dependent modules. If there's no dependents, it will be []
isForeignBooleanwhether the current module is from a foreign package.

** Properties only if isForeign is false: **

PropertyTypeDescription
codeBufferthe file content of the current module.
dependenciesArray.<walker.Module>the dependencies of the current module. If the module has no dependencies, it will be []
unsolvedDependenciesArray.<String>the array contains the items require()d by the module.

Class: walker.Error

  • code String the enum type of the error
  • message String error messages
  • stack String the origin error.stack
  • data Object the object of the major information of the error, this is useful for i18n.

Error codes

Keywords

FAQs

Package last updated on 03 May 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc