microsoft / microsoft/TypeScript

`moduleResolution: node` doesn't align with Node's module resolution when targeting ES2015.

Open
#38,729 0 comments 6 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Jun 11, 2020.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.