question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack 5 Node Interface - can't resolve loader from node_modules

See original GitHub issue

Bug report

What is the current behavior?

I’m trying to compile custom TypeScript code in Node at runtime using Webpack 5.

It says it can’t resolve ts-loader from node_modules because node_modules doesn’t exist, except it definitely does exist.

Here’s the error from the log:

[   {
    loc: 'main',
    message: "Module not found: Error: Can't resolve 'ts-loader' in 'D:\\Projects\\projectCode\\projectcode-server-test'",
    details: "resolve 'ts-loader' in 'D:\\Projects\\projectCode\\projectcode-server-test'\n" +
      '  Parsed request is a module\n' +
      '  No description file found in D:\\Projects\\projectCode\\projectcode-server-test or above\n' +
      '  resolve as module\n' +
      "    D:\\Projects\\projectCode\\projectcode-server-test\\node_modules doesn't exist or is not a directory\n" +
      "    D:\\Projects\\projectCode\\node_modules doesn't exist or is not a directory\n" +
      "    D:\\Projects\\node_modules doesn't exist or is not a directory\n" +
      "    D:\\node_modules doesn't exist or is not a directory",
    stack: "ModuleNotFoundError: Module not found: Error: Can't resolve 'ts-loader' in 'D:\\Projects\\projectCode\\projectcode-server-test'\n" +
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\webpack\\lib\\Compilation.js:1762:28\n'
+
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\webpack\\lib\\NormalModuleFactory.js:730:13\n'
+
      '    at eval (eval at create (D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\tapable\\lib\\HookCodeFactory.js:33:10), <anonymous>:8:1)\n' +
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\webpack\\lib\\NormalModuleFactory.js:273:22\n'
+
      '    at eval (eval at create (D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\tapable\\lib\\HookCodeFactory.js:33:10), <anonymous>:7:1)\n' +
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\webpack\\lib\\NormalModuleFactory.js:488:15\n'
+
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\webpack\\lib\\NormalModuleFactory.js:117:11\n'
+
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\webpack\\lib\\NormalModuleFactory.js:571:8\n'
+
      '    at D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\neo-async\\async.js:2830:7\n'
+
      '    at done (D:\\Projects\\projectCode\\projectcode-server-test\\node_modules\\neo-async\\async.js:2925:13)' } ]

If the current behavior is a bug, please provide the steps to reproduce.

Create a new project with the following files and run it:

index.js

const webpack = require('webpack');
const { createFsFromVolume, Volume } = require('memfs');

const vol = Volume.fromJSON({
    './src/index.tsx': `
        console.log('index test');
    `
});

const inputFS = createFsFromVolume(vol);
const outputFS = createFsFromVolume( new Volume() );

const compiler = webpack(
    {
        entry: './src/index.tsx',
        mode: 'production',
        module: {
            rules: [
                {
                    test: /\.tsx?$/,
                    use: 'ts-loader'
                }
            ]
        },
        resolve: {
          extensions: ['.tsx', '.ts', '.jsx', '.js'],
        },
        output: {
            filename: 'bundle.js'
        }
    }
);

compiler.inputFileSystem = inputFS;
compiler.outputFileSystem = outputFS;

compiler.run((err, stats) => {
    if (err) {
        console.error(err.stack || err);

        if (err.details) console.error(err.details);

        return;
    }

    const info = stats.toJson();

    if (stats.hasErrors()) console.error(info.errors);

    if (stats.hasWarnings()) console.warn(info.warnings);
});

package.json

{
  "name": "projectcode-server-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "memfs": "^3.2.2",
    "ts-loader": "^9.2.2",
    "webpack": "^5.37.1"
  }
}

What is the expected behavior?

node_modules should be found since it exists in the expected path, and ts-loader should be resolved.

Other relevant information: webpack version: 5.37.1 Node.js version: 12.16.2 Operating System: Windows 10 Pro Additional tools: None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
royibernthalcommented, May 25, 2021

Yup. Thanks again.

0reactions
alexander-akaitcommented, May 25, 2021

@royibernthal Can we close?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack 5 Node Interface - can't resolve loader from ...
It says it can't resolve ts-loader from node_modules because node_modules doesn't exist, except it definitely does exist (D:\Projects\ ...
Read more >
Resolve | webpack
These options change how modules are resolved. Webpack provides reasonable defaults, but it is possible to change the resolving in detail.
Read more >
To v5 from v4 - webpack
This guide aims to help you migrating to webpack 5 when using webpack directly. If you are using a higher level tool to...
Read more >
node-loader - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
expose-loader - webpack
%20 is space in a query string, because you can't use spaces in URLs ... The require.resolve call is a Node.js function (unrelated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found