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

Can't resolve 'module' in '...../node_modules/prettier' #4879

Closed
swufexlm opened this issue Jul 24, 2018 · 9 comments
Closed

Can't resolve 'module' in '...../node_modules/prettier' #4879

swufexlm opened this issue Jul 24, 2018 · 9 comments
Labels
area:api Issues with Prettier's Application Programming Interface locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Comments

@swufexlm
Copy link

Environments:

  • Prettier Version: 1.13.7
  • Running Prettier via: Node.js API
  • Runtime: Node.js v8.11.2
  • Operating System: macOS,

Steps to reproduce:

I have a project base on create-react-app, typescript@2.9.2 and wepack@3.8.1. When I try to add prettier to my project, the webpack has a compile error:

./node_modules/prettier/third-party.js
Module not found: Can't resolve 'module' in '../node_modules/prettier'

But I can find the 'module' declare in @types/node/index.d.ts:

declare module "module" {
    export = NodeJS.Module;
}

What can I do to solve this problem?

@j-f1 j-f1 added scope:external This is not an issue with Prettier, it’s an issue with external software, like an editor integration type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker. area:api Issues with Prettier's Application Programming Interface status:awaiting response Issues that require answers to questions from maintainers before action can be taken and removed scope:external This is not an issue with Prettier, it’s an issue with external software, like an editor integration type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker. labels Jul 24, 2018
@j-f1
Copy link
Member

j-f1 commented Jul 24, 2018

Hello @swufexim!

Would it be possible for you to provide a small example that reproduces the problem?

@ikatyang
Copy link
Member

This looks like a webpack issue caused by require-from-string (which is one of our sub-dependencies):

I guess this issue should probably be fixed either in require-from-string or webpack.

@swufexlm
Copy link
Author

Thanks for your reply.

Hello @j-f1
I commit an example to https://github.com/swufexlm/prettier_issue4879, which can reproduce this problem.

Hello @ikatyang
According to webpack#5423, I try to use var module$1 = module.constructor; instead of var module$1 = _interopDefault(require('module')); in third-party.js. Webpack compiles successful but I get another error:

TypeError: existsSync is not a function
at node_modules/prettier/third-party.js:5400

I think this may be caused by the setting of webpack config:

node: {
    fs: 'empty'
}

because I will run my project in the browser.

Now, I use prettier with <script src="https://unpkg.com/prettier@1.13.0/standalone.js"></script> in the browser directly and it work well.

@no-response no-response bot removed the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Jul 24, 2018
@grofit
Copy link

grofit commented Aug 7, 2018

Has this been fixed? or is there some recommended work around? I am trying to use it in electron with webpack and it keeps blowing up with same error :(

@ikatyang
Copy link
Member

ikatyang commented Aug 7, 2018

FYI, we updated cosmiconfig to v5 in #4899, which dropped the require-from-string dependency, and it'll be in 1.14.1 in few days.

@suchipi
Copy link
Member

suchipi commented Aug 8, 2018

@grofit you can either do this, to force using electron's require function to load it:

eval("require")("prettier")

Or you can require "prettier/standalone" instead of "prettier".

@swufexlm
Copy link
Author

swufexlm commented Aug 8, 2018

@grofit Now in my project, I use prettier in this way:

const prettier = require("prettier/standalone");
const prettierBabylon = require("prettier/parser-babylon");
const prettierTS = require("prettier/parser-typescript");

const content1 = prettier.format("some typescript codes", {
  parser: "typescript",
  plugins: [prettierTS]
});

const content2 = prettier.format("some json texts", {
  parser: "json",
  plugins: [prettierBabylon]
});

@grofit
Copy link

grofit commented Aug 8, 2018

ok thanks guys will give them a try.

Update

Still didnt work trying the above (tried eval and normal require) also tried adding externals in webpack config. One thing that I am doing which is possibly not normal is specifying the webpack target as "web" as I am running electron from a web server in a dev version. Anyway if this is potentially fixed in the new version I will just wait for that.

@grofit
Copy link

grofit commented Aug 20, 2018

Any news on this one? I am trying to use the csharp plugin and trying @swufexlm approach looks sensible but just blows up for me, and the plugin docs imply you should provide a path (ideally want to bundle it in with the electron stuff so it doesnt need to lookup at runtime, so the passing of the plugin object seems a nicer approach).

Any info would be great!

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Nov 18, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Nov 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:api Issues with Prettier's Application Programming Interface locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

5 participants