-
-
Notifications
You must be signed in to change notification settings - Fork 532
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
Fail to load module from node_moules #155
Comments
|
Since I'm using Typescript for my project, I also prefer Typescript modules as the dependencies. So I think it's necessary to compile dependencies or project won't work using |
Realistically, the setup you're building is kind of awkward. You're relying on a global mutation that'll be done by a dependent. Why not just compile the files to JavaScript? What about having both projects register with |
@blakeembrey thanks for point me from #158 to here, sorry for not notice this issue before I post. I do suggest ts-node to compiling dependencies. because is a good way to modulize pure typescript code if we do not want to compile .ts file any more. (it's why ts-node here, right?) @zengfenfei I ran into the same issue as you today. I have to make this work by soft link this:
more detail you can find in my dup issue #158 |
By the way, there's a similar issue here: #135. I think the correct solution, as mentioned in that issue, is to implement |
@cleavera No, they don't. You should probably do some research into the topic yourself to get familiar. TypeScript libraries should be distributed in JavaScript with declarations enabled. Please don't publish raw TypeScript libraries and fragment the ecosystem for no reason. Internally, you can do whatever you want of course. |
I had a similar problem to what @zixia had (sharing internal code between Typescript projects) which led me to this issue. Since it appears to be a design decision not to support .ts files in the Example: "scripts": {
"postinstall": "tsc -d -p ."
}, I also added a tsconfig.json file to the root folder of the library with all the compiler settings. |
The hard coded
This setup works fine until testing cannot happen because ts-node does not compile in Please give us a way to fix this. |
@gaspard You can start with the README. I have no intention of supporting bad practice by default. |
@blakeembrey Can you please explain in what sense using a public API is Using I think there are simple ways to avoid compiling vendor modules and still allow complex open source projects to use
|
@gaspard Yes, it's valid. Believe it or not, I know how the node.js module system works. I would never promote someone publishing TypeScript first class, you should be publishing JavaScript. If you've done development with node.js for a while, you'll probably remember how CoffeeScript turned out - you have a bunch of people writing |
As my perspective, I agree with @gaspard before, but I have to say, today I'll agree with @blakeembrey. Last year, I want to publish pure TypeScript module to NPM, and actually, I did. I also have a workaround about this, you could have a look at this thread at #155 (comment) . The reason I want to do this is that at first:
Today, I learned from Angular how to publish NPM in UMD format with TypeDefination, it's very easy to do this with an npm script, just as @blakeembrey said, really is a two seconds work. So I have no reason to keep my module in ts only. At last, if there's no dark side of using pure TypeScript NPM module, I believe it is possible to support it by ts-node. However, the dark side I could image is: when a javascript developer found your module from NPM and So today I vote NO for publishing pure TypeScript module to NPM. Hope this could help others like me to make the right decision today. :) |
@blakeembrey This is really a confused discussion. I will never publish a typescript module and this is not the issue. The issue is that people work inside a repository that happens to be The issue is that we cannot use For example, these are not installed modules, but the actual packages developed by Cerebral.js: https://github.com/cerebral/cerebral/tree/master/packages/node_modules |
It's exactly the same and nothing is confused here. If you check the README you can see how to get around it ( |
I am really sorry. I don't know how I missed this |
No problem 😄 Let me know if there's any way to improve the docs to avoid issues in the future. |
How does your final On the other hand, I've not found a good regex to specify a robust ignore pattern:
Any suggestions? PS: @blakeembrey: Please fix the typo in the ticket's title ("node_moules") -- it's impossible to find when searching for 'node_modules'. |
@qqilihq I decided to postpone my update of the repository until I have more time for this. If you manage to have something working, please share it here :-) there are not that many examples out there... |
@gaspard Thank you for your reply. I went for the option 2 now and spent half a day refactoring. Still fighting with lots minor issues though. If I come to a good solution, I'll be glad to share it! |
Since Here is an example for |
Thanks @romainPrignon. It seems that the same type of thing is necessary when dealing with I whitelisted our npm org and things started working:
|
This doesn't work for me. I used this command:
and get this error:
My tsconfig.json looks pretty boring:
Anyone an idea how to get this TS-only dependency working? |
Oh, I found it was related to For anyone having the same issue, I fixed it using:
|
I agree typescript shoudn't be published. I'm having issues importing ES2015 modules though. We need to use I can't compile my typescript because it imports code that supports tree-shaking... |
@hanvyj I'm not quite sure I follow or understand why it's an issue with |
It's not! Turns out you can do what I want with the |
In case it helps anyone, here's how to do it using the API (instead of CLI) with regex(es): require('ts-node').register({
typeCheck: false,
transpileOnly: true,
files: true,
// HERE, you can use RegExp literals here.
ignore: [
/node_modules\/(?!@scoped\/package)/,
/node_modules\/(?!unscoped-package)/
],
compilerOptions: require('./tsconfig.json').compilerOptions,
}) |
Node.js v13 adds new complications. For anyone stumbling here, this is what you may face if you're using Node.js v13+, but I have not found a solution yet. It seems the So although my
Now you may think "just change Here's the problem: the entry point that loads
Next, you might think "well, now just convert the import tsNode from 'ts-node'
tsNode.register({
typeCheck: false,
transpileOnly: true,
files: true,
project: './tsconfig.json',
ignore: false,
compilerOptions: {
baseUrl: './',
module: 'esnext', // out with the old, in with the new!
},
})
// require('./readem.ts') // instead of this,
import './readem.ts' // now try this. Unfortunately, now this won't work, because
TLDR: |
Same situation |
I wrote i gist of how it cloud work together (ts + esm) |
node & ts-node are not quite ready to run the imported TypeScript files directly. See TypeStrong/ts-node#155.
@Bnaya, the initial question did not relate to an ESM module (see #1007 for that), but to the direct import of a TypeScript file from a node_module. @romainPrignon's suggestion to change the default ignore pattern from @cspotcode, would it be worth adding this to the documentation? I'd think it's quite a common scenario to want skip the compilation of one's own node module and import the TypeScript files directly. |
Thank @blakeembrey for the explanation. I'll leave one more TL;DR here. In most cases it really makes sense to only publish compiled Javascript files along with Typescript type definitions. Their compilation/generation should be properly handled by the project itself and not its dependents. From the opposite side, the dependent projects should just use ready Javascript files and know nothing about their generation. If it's really needed, Typescript files inside To accept all files you can pass a pattern that doesn't match any string, e. g. It is also possible to run tools which use ts-node internally for parsing Typescript files that import code from |
ts-node options can also be specified in your tsconfig.json file:
See also, the official tsconfig JSON schema which includes our options: https://json.schemastore.org/tsconfig I can certainly review a PR that adds or updates the relevant documentation to our README. |
To allow monorepo packages to be transpiled, something like this works: // tsconfig.json - transpile all packages in the @monorepo namespace
{
"ts-node": {
"ignore": [ "/node_modules/(?!@monorepo)" ]
}
} |
I believe I've hit this issue with a project using Node.js v14. Basically, I'm trying to use a dependency with ES Modules and
// script.ts
import { BoxBufferGeometry, BufferGeometry, Group, Matrix4, Mesh, Vector3 } from 'three';
import { Geometry } from 'three/examples/jsm/deprecated/Geometry';
It seems to be the same issue described here (or have I got that wrong?) so I'm a bit surprised to see it marked as a "won't fix". Are ES Modules simply not supported, and there's no interest in supporting them? Support for ES Modules in Node.js itself has been improving in recent versions, and I'd be glad to see |
I'm also running into similar issues. I have a dependency that distributes itself compiled to ES2015 I believe, with ES modules. I'm having a difficult time messing with all of the possible Node.js and ts-node settings to try and get it to work. Fortunately for me, I control the dependency so I might just go add a commonjs compilation to it. Until this point, ts-node has generally worked wonderfully for me. The dream for me would be for ts-node to simply handle any files or dependencies that you point at it, be they JavaScript, TypeScript, CommonJS, or ES Modules. IMO ts-node should simply take an initial file as input and just make it work |
Same here... Any solution? |
Use #1007 |
The main ts file
app.ts
:The module file
node_modules/mod.ts
:app.ts
andnode_modules
are in the same directory. When I runts-node app.ts
, reports following error:The code runs normally when compiled to js using
tsc
. Version info:The text was updated successfully, but these errors were encountered: