以下是我在GitHub上的项目位置:
https://github.com/nandy2013/MERN-Stack-Dev
我只是想从具有以下实现的另一个js文件中的js文件导入方法:
is-empty.js
const isEmpty = value =>
value === undefined ||
value === NULL ||
(typeof value === 'object' && Object.keys(vale).length === 0) ||
(typeof value === 'string' && value.trim().length === 0);
module.exports = isEmpty;
register.js
//code
import isEmpty from './is-empty';
//code
片段:
C:\ Users \ 1217688 \ Desktop \ devconnector \ validation \ register.js:2从'./is-empty'导入isEmpty; ^^^^^^
SyntaxError:在Object.runInThisContext(vm.js:199:10)在createScript(vm.js:138:10)在Module._compile(module.js :)在新脚本(vm.js:51:7)意外导入令牌624:28)在Object.Module._extensions..js(module.js:671:10)在Module.load(module.js:573:32)在tryModuleLoad(module.js:513:12)在Function.Module ._load(module.js:505:3)
请帮忙!
默认情况下,node.js不支持ECMAScript的import
语句,因此import isEmpty from './is-empty';
应该编写而不是编写const isEmpty = require('./is-empty');
。
如果您更喜欢使用import
语句,则可以通过向--experimental-modules
Node添加参数来启用ECMAScript模块支持。但是请注意,它们的支持仍处于试验阶段,不建议在生产环境中使用它们。在您的情况下,您需要编辑package.json
项目文件并替换start
脚本命令以:
node --experimental-modules ./server.js
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句