简体   繁体   中英

SyntaxError: Unexpected token import - Node.js

When I run node app.js, I am presented with the following error:

import './react/features/base/react/prop-types-polyfill.js';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

I am not sure how to fix this because my application requires the import. My application did run with node_modules/.bin/webpack-dev-server and was built successfully.

Thanks

Install babel-cli and use babel node to run the script. It will work then as ES5 does not support import. You need to run it using es6.

babel node app.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM