如何使用 import &在 node.js 中导出? [英] How to use import & export in node.js?
问题描述
我有两个文件:
- app.js
- module.js
app.js 将有表达式,
app.js will have expression,
import 'foo' from './module'
//use foo..
和 module.js 会有,
and module.js will have,
export default {expression}
但是它不起作用.我目前使用 Node 7.0.0我尝试将 Webpack 与 babel-loader 和 es2015 预设一起使用,但不起作用.
But it is not working. I'm currently using Node 7.0.0 I tries using Webpack with babel-loader and es2015 preset but not working.
推荐答案
如果导入默认导出,您的导入不需要引号或大括号:
Your import does not need the quotes, or braces if importing a default export:
import foo from './module';
此外,您的导出应该类似于:
Also your export should look something like:
export default expression;
如果导出的项目被称为表达式,您将其导入为:
and if the exported item is called expression you'd import it as:
import expression from './module';
(导入非默认导出时需要大括号).
(you need the braces when importing non-default exports).
这里有很好的深入解释:
Very good in-depth explanation here:
http://www.2ality.com/2014/09/es6-modules-final.html
这篇关于如何使用 import &在 node.js 中导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!