node服务端渲染,解析不了react语法

张艺侬
  • 18

图片描述

全部报错信息:
0 info it worked if it ends with ok
1 verbose cli [ 'E:\node\node.exe',
1 verbose cli 'C:\Users\zjp\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js',
1 verbose cli 'run',
1 verbose cli 'server' ]
2 info using npm@6.3.0
3 info using node@v10.8.0
4 verbose run-script [ 'preserver', 'server', 'postserver' ]
5 info lifecycle reacttest@1.0.0~preserver: reacttest@1.0.0
6 info lifecycle reacttest@1.0.0~server: reacttest@1.0.0
7 verbose lifecycle reacttest@1.0.0~server: unsafe-perm in lifecycle true
8 verbose lifecycle reacttest@1.0.0~server: PATH: C:UserszjpAppDataRoamingnpmnode_modulesnpmnode_modulesnpm-lifecyclenode-gyp-bin;E:mySelf_projectreactTestnode_modules.bin;E:JAVAJDKbin;C:windowssystem32;E:react_studynode_modules.bin;D:node_Study;E:JAVAJDKjre;E:react_routernode_modules.bin;D:mongodbbin;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesIntelWiFibin;C:Program FilesCommon FilesIntelWirelessCommon;C:WINDOWSSystem32OpenSSH;E:node;C:UserszjpAppDataLocalatombin;D:VSCODEMicrosoft VS Codebin;C:UserszjpAppDataLocalMicrosoftWindowsApps;C:UserszjpAppDataRoamingnpm
9 verbose lifecycle reacttest@1.0.0~server: CWD: E:mySelf_projectreactTest
10 silly lifecycle reacttest@1.0.0~server: Args: [ '/d /s /c', 'node server.js' ]
11 silly lifecycle reacttest@1.0.0~server: Returned: code: 1 signal: null
12 info lifecycle reacttest@1.0.0~server: Failed to exec server script
13 verbose stack Error: reacttest@1.0.0 server: node server.js
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (C:UserszjpAppDataRoamingnpmnode_modulesnpmnode_modulesnpm-lifecycleindex.js:304:16)
13 verbose stack at EventEmitter.emit (events.js:182:13)
13 verbose stack at ChildProcess.<anonymous> (C:UserszjpAppDataRoamingnpmnode_modulesnpmnode_modulesnpm-lifecyclelibspawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:182:13)
13 verbose stack at maybeClose (internal/child_process.js:962:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:249:5)
14 verbose pkgid reacttest@1.0.0
15 verbose cwd E:mySelf_projectreactTest
16 verbose Windows_NT 10.0.17134
17 verbose argv "E:\node\node.exe" "C:\Users\zjp\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js" "run" "server"
18 verbose node v10.8.0
19 verbose npm v6.3.0
20 error code ELIFECYCLE
21 error errno 1
22 error reacttest@1.0.0 server: node server.js
22 error Exit status 1
23 error Failed at the reacttest@1.0.0 server script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

回复
阅读 1.3k
1 个回答
✓ 已被采纳

Node.js不支持ES6的import写法
要解决报错的话有几种
第一种:替换import为require
第二种:使用mjs后缀名,同时启动的时候增加flag --experimental-modules
例如:node --experimental-modules my-app.mjs
第三种:使用Babel编译,配置preset=es2015

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏