npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

module-walker

v3.0.2

Published

JavaScript module traverser

Downloads

56

Readme

Build Status

module-walker

Analyzes and walks down the dependencies from a entry of commonjs or es6 module and creates a B+ dependency tree.

  • Fully implemented File Modules of nodejs.
  • You can define what extensions should commonjs-walker fallback to by options.extensions, which will be very usefull for browser-side modules.

Install

$ npm install module-walker --save

walker(options = {})

const walker = require('module-walker')

let resolve = (nodes) => {
  // nodes
}

walker(options)
  .on('warn', message => {
    console.warn(message)
  })
  .walk(filename)
  .walk(filename)
  .then(resolve, reject)
  • nodes Object.<id>:<walker.Node>
  • resolve function(nodes)
  • reject function(err)

For the example of variable nodes, see the last section below.

options

All options are optional. Default options are typically used for node.js environment in strict mode.

  • allowCyclic Boolean=true When false, if cyclic dependencies are detected, it will be reject()ed.
  • checkRequireLength Boolean=false When true, require() method only accepts one argument. Otherwise, it will be reject()ed
  • allowAbsoluteDependency Boolean=true When false, require()ing an absolute path is not allowed, such as require('/data/a.js'), which has several issues for browser-side module.
  • extensions Array=['.js', '.json', '.node'] See options.extensions section.
  • requireResolve Boolean=true When true, require.resolve() will be parsed.
  • requireAsync Boolean=false Specially, if true, module-walker will parse the usage of require.async(id) for some browser-side module loaders.
  • allowNonLiteralRequire Boolean=true Whether should check the usage of method require(). If false, the argument of require() must be an literal string, otherwise it will be reject()ed.
  • commentRequire Boolean=false When true, it supports to write
// @require('./controller/a')
// @require('./controller/b')
const Controller = require(`./controller/${type}`)

which is really helpful for browsers

  • allowImportExportEverywhere Boolean=false By default, import and export declarations can only appear at a program's top level. Setting this option to true allows them anywhere where a statement is allowed. This option is used for babylon.
  • allowReturnOutsideFunction Boolean=false By default, a return statement at the top level raises an error. Set this to true to accept such code. This option is used for babylon.
  • sourceType String='module' Indicate the mode the code should be parsed in. Can be either "script" or "module". This option is used for babylon.
  • parse function(code, options)=walker.astFromSource Method to parse and return the ast(estree) of the given code. (probably don't use this)
  • resolve function(id, options, callback)=walker.resolve Asynchronous method to require.resolve() the given id. (probably don't use this)

options.extensions

type Array

When we require() a path, if path is not found, nodejs will attempt to load the required filename with the added extension of .js, .json, and then .node. Reference via

For browser-side environment, we could use ['.js', '.json'].

Struct: walker.Node

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

Property | Type | Description -------- | ---- | ----------- foreign | Boolean | whether the current module is from a foreign package. require | Object | The <id>: <path> map. id is the module identifier user require()d in the module file. resolve | Object | similar to require async | Object | similar to async type | Array.<String> | the type of the current node to be required. see example below.

Example

If the file structure of your project is (actually it is a very extreme scenario):

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

index.js:

require('./a')
require('b')
var image = require.resolve('./a.png')

a/index.json

{}

Code:

walker().walk('/path/to/index.js').then(function(nodes){
  console.log(nodes)
});

Then, the nodes object will be something like:

{
  '/path/to/index.js': {
    id: '/path/to/index.js',
    require: {
      './a': '/path/to/a/index.json',
      'b': 'b'
    },
    resolve: {
      './a.png': '/path/to/a.png'
    },
    code: <buffer>
    type: ['require'] // there is a 'require' type for entry node
  },
  '/path/to/a.png': {
    require: {},
    type: ['resolve'] // indicates that this node is `require.resolve()`d
  }
  '/path/to/a/index.json': {
    require: {},
    type: ['require'],
    code: <buffer>
  },
  'b': {
    foreign: true
  }
}

License

MIT