简体   繁体   English

如何导出 function 以在另一个文件脚本中调用

[英]How do I export a function to be called in another file script

 > Here is the exported code function start() { inquirer.prompt({ name: "empChoice", type: "list", message: "What would you like to do?", choices: [ "Employee View", "Department View", "Roles View", "Department View by salary totals", "Employee View by Managers", "Add Department", "Add Roles", "Add Employees", "Update Employee Roles", "Update Employee Managers", "Delete Departments", "Delete Roles", "Delete Employees", "Exit", ], }).then(function (answer) { switch (answer.empChoice) { case "Employee View": queryCalls.empFunc(); break; case "Department View": queryCalls.deptFunc(); break; case "Roles View": queryCalls.rolesView(); break; case "Department View by salary totals": deptSalaryView(); break; case "Employee View by Managers": empmanagerView(); break; case "Add Department": addDept(); break; case "Add Roles": addRoles(); break; case "Add Employees": addEmployees(); break; case "Update Employee Roles": updateEmployeeRoles(); break; case "Update Employee Managers": updateEmployeeManagers(); break; case "Delete Departments": delDept(); break; case "Delete Roles": delRoles(); break; case "Delete Employees": delEmployees(); break; case "Exit": connection.end(); break; } }); }; start(); export default start; > Here is my code from the file where I want to import the code above: // Dependencies const options = { client: 'mysql', connection: { host: '127.0.0.1', user: 'root', // db user password: 'password', // db password database: 'employees_db' // db needed } }; const knex = require('knex') (options); const cTable = require('console.table'); import start from "../app.js"; // exported functions let queryCalls = { // function Employee View empFunc: function() { knex.from('employee').select("*").then((output) => { console.log('\n'); console.table(output); } ).catch((err) => { console.log( err); throw err }).finally(() => { knex.destroy(); }) start(); }; > But I keep getting this error: (node:2393) Warning: To load an ES module, set "type": "module" in the package.json or use the.mjs extension. (Use `node --trace-warnings...` to show where the warning was created) /Users/somelinaobiechina/Desktop/EmployeeTracker/app.js:112 export default start; ^^^^^^ SyntaxError: Unexpected token 'export'.

I have tried all different modes of export but none has worked so far.我尝试了所有不同的出口模式,但到目前为止都没有奏效。 I am currently stomped:(.我目前被踩到了:(。

Thank you!谢谢!

ES 5 syntax.. ES 5 语法..


function start(){
 ....
}

function play(){
  ...
}

module.exports = start //default export

module.exports = {
 play
} // name export

and import it并导入它

const start = require('../app.js')

const { play } = require('../app.js')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 java 脚本文件中调用 function 在另一个 function 内? - How do I call a function inside of another function from java script file in html file? 如何将带有参数的函数传递给另一个要调用的函数? - How do I pass a function with arguments to another function to be called? 如何将模块模式 function 导出到另一个 JavaScript 文件 - How can I export module pattern function to another JavaScript file 如何将字符串导出到另一个Vue文件? - How do I export a string to another Vue file? 如何将此变量从类方法导出到另一个文件? - How do I export this variable from class method to another file? 当被调用函数本身在上下文中时,如何避免nodejs vm脚本函数调用丢失上下文? - How do I avoid nodejs vm script function call losing context when called function is itself in the context? 如何读取应用脚本项目中的另一个文件? - How do i read another file inside the app script project? 如何正确地将一个普通的 JS 文件导出和导入到另一个 JS 文件中? - How do I correctly export and import a plain JS file into another JS file? 如何在Node.js中正确导出函数? - How do I export a function correctly in nodejs? 我正在尝试导出一个函数以导入到另一个 javascript 页面,但无法弄清楚如何使用导出/导入来做到这一点 - i am trying to export a function to import into another javascript page but can not figure out how to do it with exports/imports
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM