-
-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SyntaxError: Unexpected token import
when installed globally and registered programmatically
#621
Comments
SyntaxError: Unexpected token import
when installed globallySyntaxError: Unexpected token import
when installed globally and registered programmatically
Maaaaaybe related to #617? |
New attempt: instead of using programmatic access, I used a child process with: #!/usr/bin/env node
const { resolve } = require('path');
const { spawn } = require('child_process');
const tsnode = spawn(
resolve(__dirname, 'node_modules', '.bin', 'ts-node'),
[
'--project',
resolve(__dirname, 'tsconfig.json'),
resolve(__dirname, 'src', 'scripts'),
],
{ stdio: [process.stdin, process.stdout, process.stderr] },
);
tsnode.on('error', data => {
console.log(`error: ${data}`);
}); Getting the exact same error (!):
|
I was confused for a bit but then realised your issue. It's because your module is inside |
Hey @blakeembrey, thanks for your reply! As you can see here, the If this is an issue on our end, how would you go about fixing it? I'm trying to figure out how |
When you make it a global it’s in node modules, therefore it is not compiled. You can see the path in the README of your example that fails. |
@blakeembrey, I'm playing with
( Do you have any suggestion to get past this? |
FYI the top of our import inquirer from 'inquirer';
import chalk from 'chalk';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { spawnSync } from 'child_process';
import request from 'request';
import { ... } from './ourStuff'; so somehow it chokes on |
Nevermind, I didn't have my
This is getting out of hand, I'm not sure why I'm getting this now. |
So we discovered recently that error occurs because TypeScript won’t actually compile anything in node modules. There’s no workaround right now. You can probably find the issue where it was discovered to learn more (I’m on mobile now). |
That isn't true, at least lately. I am able to ship private modules on NPM with source code entirely in TypeScript, then have my app compile them with TypeScript just fine. (using ts-node or ts-loader in Webpack). I am however running into this problem, with files that are not in This HTML file look like <!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<title>Visualizer</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div id="root"></div>
<script>
require("./").start()
</script>
</body>
</html> The require('ts-node').register({
typeCheck: false,
transpileOnly: true,
files: true,
ignore: [
// ignore all node_modules except package-with-ts-source
/node_modules(?!\/package-with-ts-source)/,
],
})
require('./entry') // ./entry.tsx which then throws the error
on the first line of my This all works fine when I load the HTML file using Electron's I'm stuck trying to figure out why this is happening... |
I think the discovery Blake is referring to started with this comment in #624. The thread suggests the root cause is microsoft/TypeScript#11946. |
We have been losing our hair on this for a couple days, in a rather sizable project, so I made a "minimal" reproduction repository at https://github.com/astorije/create-repro-ts-node-issue
All the details of how to reproduce, what the error is, what we tried, etc. should be explained in the README of this repo, but I'll summarize it here:
When running this repo locally, either directly by calling the script, or by using
yarn link
(mimicking global install), everything works fine.When running this globally (
yarn global add create-repro-ts-node-issue
) or usingyarn create
(yarn create repro-ts-node-issue
), it seems there is something wrong that causes Node to run running an ES module:I'm happy to provide more info, I think I provided as much as I could in the repro repo, but let me know if I missed anything.
The text was updated successfully, but these errors were encountered: