Skip to content
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

Putting a .ts file in middleware/ leads to death #2310

Closed
qm3ster opened this issue Dec 3, 2017 · 15 comments
Closed

Putting a .ts file in middleware/ leads to death #2310

qm3ster opened this issue Dec 3, 2017 · 15 comments

Comments

@qm3ster
Copy link

qm3ster commented Dec 3, 2017

nuxt version: 1.0.0-gh-704e7cd
If you put a .ts file in middleware/ while nuxt dev is running, everything is fine.
Furthermore, this file is actually compiled as typescript and can be required on pages through the middleware property.

However, if you stop the process and try to run nuxt dev again, you will get the most peculiar error:
(only) page-level components (only) with <script lang="ts"> will produce the following error:

  ERROR  Failed to compile with 1 error

 error  in ./pages/check.vueerror  in ./pages/test.vue

Module build failed: Error: Could not find file: '/test/pages/test.vue'.
    at getValidSourceFile (/test/nod
e_modules/typescript/lib/typescript.js:95642:23)

This happens for any .ts file in middleware and any <script lang="ts"> in a page, even if the middleware is empty and unused and the script tag is empty.

This question is available on Nuxt.js community (#c2017)
@clarkdo
Copy link
Member

clarkdo commented Dec 4, 2017

Can you have a look at: https://github.com/clarkdo/nuxt.js/tree/issue_2310/examples/typescript, it works for me, if anything I missed, pls tell me 😺

@Atinux
Copy link
Member

Atinux commented Dec 4, 2017

Hi @qm3ster

In 1.0, we will remove native TS support, it will have to be used in with a module, so it will remove this unexpected behavior.

@AndrewBogdanovTSS
Copy link

@Atinux I'm using nuxt-ts-starter template and I do want my middleware as .ts files, I don't want to exclude them. How can I use them? I'm currently using 1.0.0-alpha.5 and still getting this error when I try to use .ts files inside middleware folder

@qm3ster
Copy link
Author

qm3ster commented Jan 29, 2018

@AndrewBogdanovTSS
Copy link

What do you mean by "up to date" ? I'm using latest CLI build of the ts starter template? Does it need any additional configurations to support .ts files? I thought that the whole intent of this template was to provide such functionality for users out of the box

@AndrewBogdanovTSS
Copy link

AndrewBogdanovTSS commented Feb 2, 2018

@qm3ster I've updated my modules/typescript.ts file to reflect the link you've sent, now I'm getting 2 errors:
(node:2104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'push' of undefined
(node:2104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: Cannot read property 'push' of undefined
My version of node is 8.9.4 in case this matters

@qm3ster
Copy link
Author

qm3ster commented Feb 2, 2018

That's just node, you need to go higher in the call stack.
Where is this .push?

@AndrewBogdanovTSS
Copy link

There is no "higher" http://take.ms/Tv9Hz
That's all I get after running npm run dev
I suppose it happens here:

// Add .ts extension for store, middleware and more
    this.nuxt.options.extensions.push('ts')

@AndrewBogdanovTSS
Copy link

I tried to debug it deeper but it's all buggy, fixing one issue reveals another one. Feels like it's not compatible with the setup used in Nuxt TS Starter

@qm3ster
Copy link
Author

qm3ster commented Feb 2, 2018

Well, that's unhandled promise rejection for you! It turns men into vegetables and women into alcoholics.

@qm3ster
Copy link
Author

qm3ster commented Feb 2, 2018

@AndrewBogdanovTSS, my duderino...
I just tried it on latest nuxt and everything works.
The only reference to typescript is the file I linked you and the corresponding

	modules: ['~/modules/typescript.js'],

in nuxt.config.js

It then started compiling my middleware and I was able to use it.
You call it by name, right? (router: {middleware: ['test']}) You're not importing it?

@AndrewBogdanovTSS
Copy link

AndrewBogdanovTSS commented Feb 5, 2018

I just tried it on latest nuxt and everything works.

Did you tried it on the version 1.0.0-alpha.5 ? It's the one that is used by the nuxt ts starter template

@qm3ster
Copy link
Author

qm3ster commented Feb 5, 2018

Certainly not, there is no reason to use an alpha now that there's a release version.
I tried it, especially for you, and I do indeed get these errors.
It is caused by lines 3 and 22-26(24)

	this.nuxt.options.extensions.push('ts')
//...
	for (let rule of config.module.rules) {
		if (rule.loader === 'vue-loader') {
			rule.options.loaders.ts = tsLoader
		}
	}

This is because the mechanism for adding custom script loaders wasn't fully extracted yet in that version.
Just commenting them out works, but I suggest updating to the current nuxt. It's always worth it.

@AndrewBogdanovTSS
Copy link

I'm all for it, but I'm just pointing out that I had to use alpha version because it was specified in the starter template and updating to new version manually was causing my app to crash. I've created a separate issue about that here: nuxt-community/typescript-template#24

ysKuga added a commit to ysKuga/nuxt-edge-nue that referenced this issue Sep 24, 2018
- 現状 `.ts` ファイルは使用不可?
  [Putting a .ts file in middleware/ leads to death](nuxt/nuxt#2310)
@lock
Copy link

lock bot commented Nov 4, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 4, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants