Skip to main content
Add eg for mjs, code formatting
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Keep in mind that the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 
 module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

If your db.js were a module db.mjs, use a dynamic import to load the module:

node -e 'import("./db.mjs").then( loadedModule => loadedModule.init() )' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Keep in mind that the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Keep in mind that the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

 module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

If your db.js were a module db.mjs, use a dynamic import to load the module:

node -e 'import("./db.mjs").then( loadedModule => loadedModule.init() )' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... BearKeep in mymind that the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Bear in my the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Keep in mind that the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

simplified as some had issues reading
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Bear in my the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question. Indeed, it could have been avoided if you like this less-descriptive syntax:

node -e 'require("./db").()' 

To achieve that, have the 'special' default reference your function, init:

module.exports.default = init; 

Remember that's a reference to the function init, not a call to it!

You could also use a Node command line option to require the module:

node --require my-module.mjs -e 'foo'

node -r my-module.mjs -e 'foo'

... in which case you could have your module add foo to the global namespace, but that's not a nice thing to leave other developers with, so I leave that as an exercise for the twisted.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Bear in my the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question. Indeed, it could have been avoided if you like this less-descriptive syntax:

node -e 'require("./db").()' 

To achieve that, have the 'special' default reference your function, init:

module.exports.default = init; 

Remember that's a reference to the function init, not a call to it!

You could also use a Node command line option to require the module:

node --require my-module.mjs -e 'foo'

node -r my-module.mjs -e 'foo'

... in which case you could have your module add foo to the global namespace, but that's not a nice thing to leave other developers with, so I leave that as an exercise for the twisted.

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Bear in my the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () { console.log('hi'); }; 

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()' 

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

es6 just confuses peeps
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71
Loading
added 405 characters in body
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71
Loading
added 371 characters in body
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71
Loading
added 156 characters in body
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71
Loading
Source Link
Lee Goddard
  • 11.3k
  • 5
  • 53
  • 71
Loading