winnekes / itypescript

ITypescript is a typescript kernel for the Jupyter notebook (A modified version of IJavascript)
Other
188 stars 29 forks source link

`import module` is not working #8

Closed umrashrf closed 5 years ago

umrashrf commented 5 years ago

I have been struggling to import mongoose into my jupyter notebook with several different errors. I am very new to nodejs and typescript so that could be the reason as I don't understand everything.

umrashrf commented 5 years ago

https://github.com/umrashrf/notebooks/blob/master/nodejs/MongoDB%20Benchmarks.ipynb

ghost commented 5 years ago

I had the same issue and found the solution in the issues for ijavascript:

https://github.com/n-riesco/ijavascript/issues/118

If you follow those instructions and install the mongoose module in the notebook's working directory then the following import will work:

import mongoose from './node_modules/mongoose'

ghost commented 5 years ago

I should also note that I am having some inconsistency in using import. Sometimes it works, sometimes not. I am not sure if this is a problem with the kernel or with my limited understanding of typescript. Using const/require (e.g. const mongoose = require("./node_modules/mongoose") works reliably, but why not just use ijavascript at that point?

bgnkim commented 5 years ago

@umrashrf I think the import statement that you used is not for typescript/commonJS. The following will work in ITypescript 0.3.0

import * as mongoose from 'mongoose';
mongoose

or

import mongoose = require('mongoose');

@ihenry42 That was my fault. The import statement was not correctly working in the previous version of ITypescript. After fixing the bug, at v0.3.0, it works correctly as expected when I tested in my laptop. Sorry for the inconvenience.

umrashrf commented 5 years ago

I’d beg to differ. The same import works when compiled using tsc compiler.

bgnkim commented 5 years ago

It is strange, then....

One possible cause may be the transpiling procedure. Since ITypescript is based on IJavascript kernel, the typescript code is first transpiled into a javascript code. The typescript's transpiler behaves differently:


import * mongoose from 'mongoose';
mongoose;

This code becomes:

"use strict";
Object.defineProperty(exports, "__esModule", {value: true});
var mongoose = require("mongoose");
mongoose

However,

import mongoose from 'mongoose';
mongoose;

This code becomes:

"use strict";
Object.defineProperty(exports, "__esModule", {value: true});
var mongoose_1 = require("mongoose");
mongoose_1.default

To verify this output, you can run the simple transpiler code in node.js as follows:

const ts = require('typescript');
ts.transpile([code to be transpiled]);

I found that running two transpiled javascript codes generates different results in NodeJS.

umrashrf commented 5 years ago

The documented use of mongoose is import mongoose from 'mongoose' https://www.npmjs.com/package/mongoose#importing

umrashrf commented 5 years ago

I wonder what's the javascript version used by IJavaScript. I hope it supports ES6.

bgnkim commented 5 years ago

Before testing the issue with ES6, I think IJavascript can run ES6 code (since it evalutes code using node, roughly speaking). However the code tested before was transpiled with ES5 feature, by default.

Although I'll run this code using ES6 soon, could you run the same code with ES6 feature enabled in tsconfig.json? Or, please upload your tsconfig.json that you're currently using.

umrashrf commented 5 years ago

While on this, I came across another issue regarding parsing of tsconfig.json https://github.com/nearbydelta/itypescript/issues/10

bgnkim commented 5 years ago

After the test, I found that ES5/6 is not the problem. The "esModuleInterop" option is the problem. I'll make the default value for "esModuleInterop" option as true.

bgnkim commented 5 years ago

Now it will check whether "esModuleInterop" is set correctly. If not, it will warn you.

umrashrf commented 5 years ago

In my tsconfig.json I already had esModuleInterop: true

I still get the same error.

umrashrf commented 5 years ago

I removed moduleResolution from tsconfig.json and now I get this error https://colab.research.google.com/drive/1p1FaJE0e61yqDEq8Rd7BxvhnXk-bPIVT

Error: File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/dom.ts' not found.

File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/es2017.ts' not found.

Line 1, Character 1
import mongoose from 'mongoose'
^
'mongoose' is declared but its value is never read.
    at execTranspile (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/lib/kernel.js:190:27)
    at Session.transpiler [as transpile] (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/lib/kernel.js:229:37)
    at Session._runNow (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:796:39)
    at Session._run (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:766:14)
    at Session.execute (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:890:10)
    at Kernel.execute_request (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/jp-kernel/lib/handlers_v5.js:116:18)
    at Kernel.onShellMessage (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/jp-kernel/lib/jp-kernel.js:285:41)
    at Socket.<anonymous> (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/jmp/index.js:350:17)
    at Socket.emit (events.js:189:13)
    at Socket._emitMessage (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/zeromq/lib/index.js:640:15)
bgnkim commented 5 years ago

I think the tsconfig.json file that you uploaded in #10 is not the same that you're actually working with. Neither 'esModuleInterop', 'moduleResolution' nor 'noUnusedLocals' is listed in that code snippet, but I now wonder whether you're using tsconfig.json file with 'noUnusedLocals' options. (Since the error message might be caused by that option: refer typescript doc). Is that the configuration in #10 listing all options that you're using?

And, could you test the code suppressing tsconfig.json options temporally, like:

%noUnusedLocals false
%esModuleInterop true

import mongoose from 'mongoose';
mongoose;
umrashrf commented 5 years ago

File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/dom.iterable' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.

Error: File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/dom.ts' not found.

File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/es2017.ts' not found.

File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/scripthost.ts' not found.

File '/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/typescript/lib/dom.iterable' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
    at execTranspile (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/lib/kernel.js:190:27)
    at Session.transpiler [as transpile] (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/lib/kernel.js:229:37)
    at Session._runNow (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:796:39)
    at Session._run (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:766:14)
    at Session.execute (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:890:10)
    at Kernel.execute_request (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/jp-kernel/lib/handlers_v5.js:116:18)
    at Kernel.onShellMessage (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/jp-kernel/lib/jp-kernel.js:285:41)
    at Socket.<anonymous> (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/jmp/index.js:350:17)
    at Socket.emit (events.js:189:13)
    at Socket._emitMessage (/Users/umair/.nvm/versions/node/v10.15.1/lib/node_modules/itypescript/node_modules/zeromq/lib/index.js:640:15)
umrashrf commented 5 years ago

Looks like it is working now. That's what I did

$ npm install -g typescript
$ npm install -g itypescript
$ its --install=global
$ its

I will test more soon and update you.

$ nvm --version
0.34.0
$ node --version
v10.15.1