import thenFs from 'then-fs'报错的问题

211 阅读1分钟

用require可以引入第三方模块,但是用import引入会报错

import thenFs from 'then-fs'

thenFs.readFile('./file/1.txt', 'utf8').then(res => {
  console.log(res)
  return thenFs.readFile('./file/2.txt', 'utf8')
}).then(res => {
  console.log(res)
  return thenFs.readFile('./file/3.txt', 'utf8')
}).then(res => {
  console.log(res)
})

Snipaste_2022-05-22_21-22-46.png

原因:配置文件package.json里面添加里面的默认导出模式是"type": "commonjs",用ES6的import就会报错

解决办法: 在package.json里面添加"type": "module",设置成ES6的导出方式

Snipaste_2022-05-22_21-29-01.png