Unable to find module in `node_modules` only when targeting ES6, ES5 works fine
benlesh opened this issue · 8 comments
TypeScript Version: 1.8.10 and higher
Code
Actual code that causes the problem can be found on this issue: ReactiveX/rxjs#1637
# clone the repo
git clone git@github.com:ReactiveX/rxjs.git
# get PR branch
git fetch master pull/1637/head:pr-1637
git checkout pr-1637
# install dependencies (runs build, too, which will fail, but that's okay)
npm i
# try building the CJS version (success)
npm run build_cjs
# try building the ES6 version (failure)
npm run build_es6
The exact scripts being run are:
tsc typings/main/ambient/es6-shim/index.d.ts ./dist/cjs/src/Rx.ts ./dist/cjs/src/Rx.KitchenSink.ts ./dist/cjs/src/Rx.DOM.ts ./dist/cjs/src/add/observable/of.ts -m commonjs --sourceMap --outDir ./dist/cjs --target ES5 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors
tsc ./dist/es6/src/Rx.ts ./dist/es6/src/Rx.KitchenSink.ts ./dist/es6/src/Rx.DOM.ts ./dist/es6/src/add/observable/of.ts -m es2015 --sourceMap --outDir ./dist/es6 --target ES6 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors
Expected behavior:
Both build successfully.
Actual behavior:
Only the CJS build succeeds, the ES6 build fails:
11 import * as observableSymbol from 'symbol-observable';
~~~~~~~~~~~~~~~~~~~
dist/es6/src/Observable.ts(11,35): error TS2307: Cannot find module 'symbol-observable'.
16 import * as observableSymbol from 'symbol-observable';
~~~~~~~~~~~~~~~~~~~
dist/es6/src/observable/FromObservable.ts(16,35): error TS2307: Cannot find module 'symbol-observable'.
11 import * as observableSymbol from 'symbol-observable';
~~~~~~~~~~~~~~~~~~~
dist/es6/src/util/subscribeToResult.ts(11,35): error TS2307: Cannot find module 'symbol-observable'.
142 import * as observable from 'symbol-observable';
~~~~~~~~~~~~~~~~~~~
dist/es6/src/Rx.ts(142,29): error TS2307: Cannot find module 'symbol-observable'.
cc @kwonoj @jayphelps for visibility
I noticed specifying moduleResolution
(--moduleResolution node
) makes es15 build works, but not sure why CJS doesn't affected. (on 1.8.10)
I noticed specifying moduleResolution (--moduleResolution node) makes es15 build works, but not sure why CJS doesn't affected. (on 1.8.10)
Is that the answer then? Perhaps targeting commonjs forces that behavior?
Perhaps targeting commonjs forces that behavior?
: maybe? at least I could apply that explicitly on build script to ensure for immediate resolution.
"there's a CLI flag for that"™
Closing for now, I guess.
This is the expected behavior. The ES6 module spec does not say "Go look in node_modules
for a matching folder", so --module es6
doesn't do that either. Since you're loading modules from node_modules
, commonjs
is the correct module target for your project.
Yes, that's indeed the behavior of their code:
if (moduleResolution === undefined) {
moduleResolution = getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS ? ModuleResolutionKind.NodeJs : ModuleResolutionKind.Classic;
if (traceEnabled) {
trace(host, Diagnostics.Module_resolution_kind_is_not_specified_using_0, ModuleResolutionKind[moduleResolution]);
}
}