module-assign v0.0.1
Module Assign
Assign a local javascript file to a module of it's very own. Have you ever wanted to require() a file absolutely as a module, but without installing or linking it? This program has you covered.
npm install module-assign -gWhy
Normally if you wanted to include the local file /lib/index.js assuming you're in the root directory you would do this.
var moduleAssign = require('./module-assign.js')Note the .. you'd have to go one directory up. This is how relative linking works. However I sought out to be able to just call this:
var moduleAssign = require('module-assign')So I asked myself. How could this be done? First off npm has a link command. It creates a symlink of a package within your project's node_modules folder. However it has one major flaw it doesn't save references to local files in package.json.
I created module-harvest (a different module) to solve the specific problem of partitioning a module out of another project. Which solves this whole linking problem in a different way, it creates the module outright, however the linking problem still existed, so I created this to remedy it.
This project uses the property localDependencies within package.json to save references to localDependencies.
Usage
Below we run the command and pass in a file and use tree and json to view the results of running the command.
module-assign ./arr-camelize.js
# inspect the newly changes / created files
tree ./node_modules/arr-camelize
# ./node_modules/arr-camelize
# └── package.json
cat ./node_modules/arr-camelize/package.json | json
# {
# "name": "arr-camelize",
# "main": "../../arr-camelize.js",
# "assignedModule": true
# }
cat package.json | json localDependencies.arr-camelize
# ./arr-camelize.jsThis does the following:
- Adds the directory
arr-camelizewithinnode_modules. - Creates a module
package.jsonwithnameandmain - Adds
arr-camelizeto thelocalDependenciesobj within./package.json
If ran with DEBUG=* you can have a more verbose output
DEBUG=* ./bin/module-assign.js ./arr-camelize.js
# module-assign file read package.json +0ms
# module-assign dirs made node_modules/arr-camelize +7ms
# module-assign file written node_modules/arr-camelize/package.json +4ms
# module-assign file written ./package.json +11msAssign Modules in localDependencies
Let's say you want to assign all the modules in localDependencies at once. Sort of link npm install does. Use the command command with the --install flag.
module-assign --installLet's say you want to deploy code that uses localDependencies. Now you can have a hook in your scripts that runs module-assign --install and will assign all the deps so the code runs as expected.
10 years ago