microsoft / microsoft/TypeScript
`moduleResolution: node` doesn't align with Node's module resolution when targeting ES2015.
@weswigham ci sta già lavorando.
Dal 11/6/2020.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Valutazione
Questa issue non è ancora stata valutata.