23 Dec 2023 - tianyw0
我执行js文件报错:
➜ genName git:(main) ✗ node ./init_DB/cchar_DB_init_backup.js
file:///Users/tianyw/repo/genName/init_DB/cchar_DB_init_backup.js:1
const { pinyin, convert } = require("pinyin-pro");
^
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/tianyw/repo/genName/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
require写法是 commonjs script 的写法,import是ES module的写法,commonjs脚本需要使用.cjs后缀,ES module脚本需要使用.mjs后缀。
requirejs用于服务端nodejs
"type": "module" 配置这个字段相当于定义了项目js文件类型,上述三种js中cjs和mjs两种,可以由package.json中的type字段来定义, 也可以由文件后缀名来定义, type字段默认是commonjs,即默认是commonjs脚本
非常有意思,js中有丰富的函数式操作方法,map reduce filter,以及lodash提供的更多的方法;
非常喜欢react的纯函数思维方式——一种html in js方法; 看好stylex,一种css in js方法;