microsoft / microsoft/TypeScript
`moduleResolution: node` doesn't align with Node's module resolution when targeting ES2015.
@weswigham ya está trabajando en esto.
Desde el 11/6/2020.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
TL;DR: When resolving CommonJS modules in NodeJS, it will implicitly look for an index.js file within a folder if the folder matches the import path. When resolving ES2015 modules in NodeJS however, this is not true. Currently, TypeScript will always implicitly resolve index.js in a folder matching the provided import path, even if module is ES2015. This results in TypeScript spitting out JS without error when there is no runtime that will execute the code.
TypeScript Version: 3.9.3
Search Terms:
moduleResolution node es modules
Code
// tsconfig.json
{
"compilerOptions": {
"module": "ES2015",
"moduleResolution": "node",
}
}
// apple/index.ts
export const apple = 'red'
// index.ts
import { apple } from './apple'
Expected behavior:
An error indicating that module ./apple could not be found.
Actual behavior:
No error and it emits import { apple } from './apple'.
Related Issues:
The opposite request (declined): #32432
Tangentially related to all of the discussions around TypeScript appending .js extensions.
Thanks to @bouzuya for the following script showing module resolution in NodeJS when resolving ES2015 modules vs CommonJS modules:
#!/bin/bash
if [ -z "$1" ]; then echo 'Usage: ./node-esm-test.sh <0|1|2|3|4|5>'; exit 1; fi
echo 'node --version'
node --version # v14.2.0
case $1 in
0)
echo "case 0: Node.js CJS resolves './add' -> './add/index.js'"
mkdir node-esm-test
cd node-esm-test
mkdir add
echo 'exports.add = function(a, b) { return a + b; };' > add/index.js
echo 'const { add } = require("./add"); console.log(add(1, 2) === 3 ? "OK" : "NG");' > mod.js
# OK
node mod.js || true
cd ..
rm -rf node-esm-test
;;
1)
echo "case 1: Node.js ESM resolves './add/index.js' -> './add/index.js'"
mkdir node-esm-test
cd node-esm-test
mkdir add
echo 'export function add(a, b) { return a + b; }' > add/index.js
echo 'import { add } from "./add/index.js"; console.log(add(1, 2) === 3 ? "OK" : "NG");' > mod.js
echo '{"type":"module"}' > package.json
# OK
node mod.js || true
cd ..
rm -rf node-esm-test
;;
2)
echo "case 2: Node.js ESM doesn't resolve './add/index' -> './add/index.js'"
mkdir node-esm-test
cd node-esm-test
mkdir add
echo 'export function add(a, b) { return a + b; }' > add/index.js
echo 'import { add } from "./add/index"; console.log(add(1, 2) === 3 ? "OK" : "NG");' > mod.js
echo '{"type":"module"}' > package.json
# cannot find module ... Did you mean to import ../add/index.js?
node mod.js || true
cd ..
rm -rf node-esm-test
;;
3)
echo "case 3: Node.js ESM doesn't resolve './add' -> './add/index.js'"
mkdir node-esm-test
cd node-esm-test
mkdir add
echo 'export function add(a, b) { return a + b; }' > add/index.js
echo 'import { add } from "./add"; console.log(add(1, 2) === 3 ? "OK" : "NG");' > mod.js
echo '{"type":"module"}' > package.json
# cannot find module ... Did you mean to import ../add/index.js?
node mod.js || true
cd ..
rm -rf node-esm-test
;;
4)
echo "case 4: Node.js ESM doesn't resolve './add.js' -> './add/index.js'"
mkdir node-esm-test
cd node-esm-test
mkdir add
echo 'export function add(a, b) { return a + b; }' > add/index.js
echo 'import { add } from "./add.js"; console.log(add(1, 2) === 3 ? "OK" : "NG");' > mod.js
echo '{"type":"module"}' > package.json
# cannot find module
node mod.js || true
cd ..
rm -rf node-esm-test
;;
5)
echo "case 5: Node.js ESM doesn't resolve './add' -> './add/index.mjs'"
mkdir node-esm-test
cd node-esm-test
mkdir add
echo 'export function add(a, b) { return a + b; }' > add/index.mjs
echo 'import { add } from "./add"; console.log(add(1, 2) === 3 ? "OK" : "NG");' > mod.mjs
# cannot find module
node mod.mjs || true
cd ..
rm -rf node-esm-test
;;
esac
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Evaluación
Este issue todavía no se ha evaluado.