markdownããhtmlã«å¤æããããªã¼ã¨æãã
micromarkã使ããã¨ãããã
ES Modulesã§ããªãããã£ãã®ã§ããã®æã®åå¿é²ã
ã¯ãã¾ã
ä»ã¾ã§ä½¿ã£ã¦ãããã¡ãã£ã¨ãããã¼ã«ã®ããã¸ã§ã¯ãã«ã
micromarkãã¤ã³ã¹ãã¼ã«ãããããããªã¨ã©ã¼ããã
$ npx ts-node sample.ts ./node_modules/ts-node/dist/index.js:842 return old(m, filename); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module ./src/node_modules/micromark/index.js from ./src/sample.ts not supported. Instead change the require of index.js in src/sample.ts to a dynamic import() which is available in all CommonJS modules. at Object.require.extensions.<computed> [as .js] (./node_modules/ts-node/dist/index.js:842:20) at Object.<anonymous> (./src/sample.ts:3:21) at Module.m._compile (./src/node_modules/ts-node/dist/index.js:848:29) at Object.require.extensions.<computed> [as .ts] (./src/node_modules/ts-node/dist/index.js:850:16) at phase4 (./src/node_modules/ts-node/dist/bin.js:414:16) at bootstrap (./src/node_modules/ts-node/dist/bin.js:49:12) at main (./src/node_modules/ts-node/dist/bin.js:32:12) at Object.<anonymous> (./src/node_modules/ts-node/dist/bin.js:526:5) { code: 'ERR_REQUIRE_ESM' }
ãããã«ãmicromarkã®README.mdã«ã¯ã
This package is ESM only.
ã¨æ¸ããã¦ããã®ã§ãããã£ã½ãã
tsconfig.json
ã¯ãããªæãã
{ "compilerOptions": { "target": "esnext", "module": "commonjs", "moduleResolution": "node", "lib": ["esnext", "esnext.asynciterable", "dom"], "resolveJsonModule": true, "esModuleInterop": true } }
ES Modulesã¨CommonJS
ES Modulesã¨CommonJSã¯ãã¢ã¸ã¥ã¼ã«ã·ã¹ãã ãããã
- ESMã¯ES2015ããä»æ§ã«å ¥ã£ãæ°ãããã¤
- CommonJSã¯ã¦ã§ããã©ã¦ã¶ç°å¢å¤ã対象ã«ããå¤ããã¤
ãã£ããã ã¨ã以ä¸ã®ãããªæ¸ãæ¹ã
// ESM import { micromark } from "micromark"; // CommonJS const { micromark } = require("micromark");
TypeScriptãã使ã£ã¦ããªãã®ã§ããã®ãããã¯æ°ã«ãããã¨ããªãã
常ã«import
ã使ã£ã¦æ¸ãã¦ããã®ã§ãæ°ã«ãããã¨ããªãã£ããã
ãã®ãããã¯ã以ä¸ã®è¨äºãã¨ã¦ãåèã«ãªã£ãã
- CommonJSã¨ES Modulesã«ã¤ãã¦ã¾ã¨ãã
- TypeScript 4.7 㨠Native Node.js ESM | by Yosuke Kurami | May, 2022 | Medium
- TypeScript 4.5 以é㧠ESM 対å¿ã¯ã©ããªãã®ãï¼
ãªã«ãã ãã ã£ãã
以ä¸ã®è¨äºã§ããããããã¾ã¨ãã¦ãããã
CJSããESMãStatic Importã§ããªããã¨ãåé¡ã®ããã
TypeScript 4.7 㨠Native Node.js ESM | by Yosuke Kurami | May, 2022 | Medium
import(require) ãããã¡ã¤ã« import(require) ããããã¡ã¤ã« Static Import Dynamic Import require ESM ESM OK OK NG CJS CJS NG NG OK ESM CJS OK NG NG CJS ESM NG OK NG
ãªã®ã§ãããã¸ã§ã¯ãèªä½ãESMåãã¦ã¿ãã
ESM対å¿
ã¨ãããã¨ã§ãESM対å¿ããã¦ã¿ãã以ä¸ã®è¨äºãåèã«ãªã£ãã
ã» Pure ESM package
JavaScript(Node.js)å´ã¨TypeScriptå´ã§ãããããããã¨ãç°ãªãã
- CommonJSããã¸ã§ã¯ããESMã«å¤æ´ãã
- Node.js 14以éãã¤ãã
package.json
ã«"type": "module"
ã追årequire()
ã®æé¤- importã«æ¡å¼µåã追å
- TypeScriptããESMå½¢å¼ã§åºåãã
- TypeScript 4.7以éã使ã
- tsconfig.jsonã«
"module": "esnext"
ã追å node --loader ts-node/esm ./my-script.ts
ã使ã£ã¦å¼ã³åºã
ã¾ããESMã«ããã¨ã__dirname
ã__filename
ã使ããªããªãã®ã§æ³¨æã
Node.js / TypeScriptã®ãã¼ã¸ã§ã³ã¢ãã
ãããããã¼ã¸ã§ã³ã決ã¾ã£ã¦ããã®ã§ã¢ãããã¼ããã¦ãã
- Node.js 14以éãã¤ãã
- TypeScript 4.7以éã使ã
package.jsonã®å¤æ´
ESMã®ããã¸ã§ã¯ãã¨ãã¦èªèãã¦ããããããã
"type": "module"
ã追å ã
ã¤ãã§ã«ãloaderã使ã£ã¦å¼ã³åºããããã«ã
scripts
ã«ãts-esm
ã追å ã
{ + "type": "module", "scripts": { "ts": "npx ts-node", + "ts-esm": "node --loader ts-node/esm" } }
tsconfig.jsonã®å¤æ´
TypeScriptããESMå½¢å¼ã§åºåã§ããããã
module
ãesnext
ã«å¤æ´ããã
(ES2020
ã§ãOKãES2015
ã§ããããimport.meta
ãªã©ä½¿ããªã)
{ "compilerOptions": { "target": "esnext", - "module": "commonjs", + "module": "esnext", "moduleResolution": "node", "lib": ["esnext", "esnext.asynciterable", "dom"], "resolveJsonModule": true, "esModuleInterop": true } }
ES2022
ãnode12
ãnode16
ãnodenext
ãªã©ã®è¨è¼ããããã
ç¾æç¹ã§ã¯nightly buildsã®ã¿ã対å¿ãã¦ããªãç¶æ³ã£ã½ãã
ã»TypeScript: TSConfig Reference - Docs on every TSConfig option
ã»ESM support: soliciting feedback · Issue #1007 · TypeStrong/ts-node
require()
ã®æé¤
require()
ã¯CommonJSã®æ¸ãæ¹ãªã®ã§ã
ãã使ã£ã¦ãããimport
å½¢å¼ã«æ¸ãç´ãã
ãã ãimportã§ã¯.json
ã®æ¡å¼µåãåãä»ãã¦ãããªããã
import json from "./package.json";
./node_modules/ts-node/dist-raw/node-internal-modules-esm-get_format.js:92 throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); ^ CustomError: ERR_UNKNOWN_FILE_EXTENSION .json ./package.json ...
ãªã®ã§ãcreateRequireã§require
ãç¨æããã
import { createRequire } from "module"; const require = createRequire(import.meta.url); const json = require("./package.json");
ã»How to import JSON files in ES modules (Node.js) | Stefan Judis Web Development
"module"
ãè¦ã¤ãããªããã¨ãããã®ã§ã
@types/node
ã確èªãããv12以éãããªãã¨ã ãã£ã½ãã
import { createRequire } from "module";
importã«æ¡å¼µåã追å
ESMã®å ´åãimportæã«æ¡å¼µåãå¿
é ã«ãªãã
ãªã®ã§ãå
é¨ã®ãã¡ã¤ã«ã«ã¯ã.js
ã追å ãã¦ããã
- import { util_func } from "./my-module/utils"; + import { util_func } from "./my-module/utils.js";
æ¡å¼µåããªãã¨ããããªæãã§ã¨ã©ã¼ã«ãªãã
./node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366 throw new ERR_MODULE_NOT_FOUND( ^ CustomError: Cannot find module './src/my-module/utils' imported from ./src/sample.ts
æ¡å¼µåã¯.ts
ã§ãã£ãã¨ãã¦ãã.js
ã§æ¸ããªãã¨ãããªãã
.mts
ã®å ´åã¯ã.mjs
ã§æ¸ãã
(ãã¾ã) CJSç¹ææ©è½ã®ç½®ãæã
CJSã«ããç¡ãæ©è½ã®ç½®ãæãä¾ã
// require import { createRequire } from "module"; const require = createRequire(import.meta.url);
// __filename / __dirname import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename);
以ä¸!!
ãããã2è¡ã®ããã°ã©ã ã®ããã«ããããæ²¼ã«ããã£ã¦ãã£ããã(*´Ïï½*)
import { micromark } from "micromark"; console.log(micromark("## Hello, *world*!")); // => <h2>Hello, <em>world</em>!</h2>
åèã«ãããµã¤ããã¾
- Pure ESM package
- ESM support: soliciting feedback · Issue #1007 · TypeStrong/ts-node
- javascript - Typescript/Node: Error [ERR_MODULE_NOT_FOUND]: Cannot find module - Stack Overflow
- Node.js Dual Packages (CommonJS/ES Modules) ã«å¯¾å¿ãã npm ããã±ã¼ã¸ã®éçº - Cybozu Inside Out | ãµã¤ãã¦ãºã¨ã³ã¸ãã¢ã®ããã°
- Node.js 㧠TypeScript/ESM native ãªç°å¢æ§ç¯
- How to Use ECMAScript Modules in Node.js
ts-node
fails when ES Modules are in the dependency graph in Node.js 13+ · Issue #935 · TypeStrong/ts-node- ts-nodeã§ESModulesã®ãã¡ã¤ã«ãå®è¡ãã
- CommonJSã¨ES Modulesã«ã¤ãã¦ã¾ã¨ãã
- Node.jsã©ã¤ãã©ãª/ãã¼ã«ãESMã«ç§»è¡ãã[Node.js 12+]
- TypeScript 4.7 㨠Native Node.js ESM | by Yosuke Kurami | May, 2022 | Medium
- TypeScript 4.5 以é㧠ESM 対å¿ã¯ã©ããªãã®ãï¼
- How to import JSON files in ES modules (Node.js) | Stefan Judis Web Development