Skip to content

CoooooLer/node_es6_study

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node_es6_study

node版本:13.2.0

喜大普奔node支持es6模块导入导出

导入模块不使用require(xx) X

导入模块使用import xx from 'xx'

ReferenceError: __filename is not defined解决方法

ReferenceError: __dirname is not defined解决方法

  • 在项目目录控制终端执行命令npm init -y创建 package.json文件,并添加一句"type": "module"
1: 利用 import.meta
import { fileURLToPath } from 'url'
import { dirname } from 'path'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

console.log(__filename)
console.log(__dirname)

详情点击 import.meta 查看

2:
const __dirname = path.resolve()
const __dirname = process.cwd()
const __dirname = fs.realpathSync('.')
const __dirname = process.env.PWD

About

node_13.2.0

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published