I'm learning how NodeJS's require()
function works to better understand the node ecosystem.
I wrote a script that uses module._pile to pile a script. When I try to require()
within the string, it balks... but it seems to have the right paths. What am I doing wrong?
I guess it's worth mentioning I have the json3
module installed in the same directory in ./node_modules/
.
The Script
I wrote this script, which works--until I unment the line that's marked.
"use strict";
var json3 = require('json3');
console.log("require.main.filename:", require.main.filename);
console.log("require.main:", require.main);
var module = new module.constructor();
module._pile(`
"use strict";
console.log("require.main.filename:", require.main.filename);
console.log("require.main:", require.main);
// When I unment this, it breaks. Why?
//var json3 = require('json3');
module.exports = {
run: function () { console.log("It works!") }
};
`, require.main.paths[0]);
module.exports.run();
Script Output
Here's the output (when the problem code is mented out).
require.main.filename: /home/user/tmp/tmp.js
require.main: Module {
id: '.',
exports: {},
parent: null,
filename: '/home/user/tmp/tmp.js',
loaded: false,
children:
[ Module {
id: '/home/user/tmp/node_modules/json3/lib/json3.js',
exports: [Object],
parent: [Circular],
filename: '/home/user/tmp/node_modules/json3/lib/json3.js',
loaded: true,
children: [],
paths: [Array] } ],
paths:
[ '/home/user/tmp/node_modules',
'/home/user/node_modules',
'/home/node_modules',
'/node_modules' ] }
require.main.filename: /home/user/tmp/tmp.js
require.main: Module {
id: '.',
exports: {},
parent: null,
filename: '/home/user/tmp/tmp.js',
loaded: false,
children:
[ Module {
id: '/home/user/tmp/node_modules/json3/lib/json3.js',
exports: [Object],
parent: [Circular],
filename: '/home/user/tmp/node_modules/json3/lib/json3.js',
loaded: true,
children: [],
paths: [Array] } ],
paths:
[ '/home/user/tmp/node_modules',
'/home/user/node_modules',
'/home/node_modules',
'/node_modules' ] }
Why won't the module within the ad hoc module load?
I'm learning how NodeJS's require()
function works to better understand the node ecosystem.
I wrote a script that uses module._pile to pile a script. When I try to require()
within the string, it balks... but it seems to have the right paths. What am I doing wrong?
I guess it's worth mentioning I have the json3
module installed in the same directory in ./node_modules/
.
The Script
I wrote this script, which works--until I unment the line that's marked.
"use strict";
var json3 = require('json3');
console.log("require.main.filename:", require.main.filename);
console.log("require.main:", require.main);
var module = new module.constructor();
module._pile(`
"use strict";
console.log("require.main.filename:", require.main.filename);
console.log("require.main:", require.main);
// When I unment this, it breaks. Why?
//var json3 = require('json3');
module.exports = {
run: function () { console.log("It works!") }
};
`, require.main.paths[0]);
module.exports.run();
Script Output
Here's the output (when the problem code is mented out).
require.main.filename: /home/user/tmp/tmp.js
require.main: Module {
id: '.',
exports: {},
parent: null,
filename: '/home/user/tmp/tmp.js',
loaded: false,
children:
[ Module {
id: '/home/user/tmp/node_modules/json3/lib/json3.js',
exports: [Object],
parent: [Circular],
filename: '/home/user/tmp/node_modules/json3/lib/json3.js',
loaded: true,
children: [],
paths: [Array] } ],
paths:
[ '/home/user/tmp/node_modules',
'/home/user/node_modules',
'/home/node_modules',
'/node_modules' ] }
require.main.filename: /home/user/tmp/tmp.js
require.main: Module {
id: '.',
exports: {},
parent: null,
filename: '/home/user/tmp/tmp.js',
loaded: false,
children:
[ Module {
id: '/home/user/tmp/node_modules/json3/lib/json3.js',
exports: [Object],
parent: [Circular],
filename: '/home/user/tmp/node_modules/json3/lib/json3.js',
loaded: true,
children: [],
paths: [Array] } ],
paths:
[ '/home/user/tmp/node_modules',
'/home/user/node_modules',
'/home/node_modules',
'/node_modules' ] }
Why won't the module within the ad hoc module load?
Share Improve this question edited Jun 14, 2020 at 12:42 CommunityBot 11 silver badge asked Sep 21, 2017 at 18:57 Sir RobertSir Robert 4,9768 gold badges45 silver badges63 bronze badges1 Answer
Reset to default 8 +50So two things in your code. When you do
var module = new module.constructor();
You loose the original module object, you shouldn't do that. Next to know what the exact issue is, you should have set environment variable NODE_DEBUG=module
. This will give you what happens in background. Doing that you get an import message
MODULE 11063: looking for "json3" in []
This means there are no paths set for module loading. So you should add the paths also. Below is the updated code
"use strict";
var json3 = require('json3');
var os = require("os");
console.log("require.main.filename:", require.main.filename);
console.log("require.main:", require.main);
var _module = new module.constructor();
_module.paths = module.paths;
_module._pile(`
"use strict";
// console.log("require.main.filename:", require.main.filename);
// console.log("require.main:", require.main);
//When I unment this, it breaks. Why?
var json3 = require('json3');
module.exports = {
run: function () { console.log("It works!") }
};
`, __dirname + "/dynamic_code_loaded_at_run_time.js");
_module.exports.run();
Also the second parameter of _pile
is the filename which is being loaded, so you should not provide it a path but a filename. And now you get the correct output
It works!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385040a4625401.html
评论列表(0条)