There should be only one default value in a module. There are two ways to export it.
export default function(n, m) {
return n + m;
}
another way
function add(n, m) {
return n + m;
}
export default add;
Assume the module name is math.js. To import it,
import sum from "./math.js";
Noted that we can give any name for default value when import it. Also do not need curly braces. For not default values, need curly braces.
For no default function export or import, need curly braces
import {mymethod} from '../../utilities/actions'
......
export {foo, awesom, check}
Import multiple functions from a module
import {get, set} as moduleName from 'module-name'
moduleName.get();
//or can do
import * as moduleName from 'module-name';
moduleName.get();
//or can do. But this may cause name collision
import {get,set} from 'module-name';
get()
//real life example
import * as yup from "yup";
username: yup.string().required("Username is required"),
(node:46383) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
If get the above error when use node to run the script. The warning message you are encountering indicates that you are trying to load an ES (ECMAScript) module in a Node.js environment, but your current setup is not configured to recognize it as such.
node use commonjs module format
To solve it, one: Use "type": "module" in package.json. The alternative way is: rename your JavaScript files to use the .mjs extension instead of .js.
No comments:
Post a Comment