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.

"Building for Production" docs "Library Mode" section mildly broken

See original GitHub issue

Describe the bug

Based on;

npm create vite@latest myapp --template vanilla
cd myapp

echo "import { resolve } from 'path'
import { defineConfig } from 'vite'
export default defineConfig({
  build: {
    lib: {
      entry: resolve(__dirname, 'lib/main.js'),
      name: 'MyLib',
      // the proper extensions will be added
      fileName: 'my-lib'
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ['vue'],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          vue: 'Vue'
        }
      }
    }
  }
})" > vite.config.js

mkdir lib
echo "export default function square(n) {
  return n*n;
}" > lib/main.js

echo '{
  "name": "my-lib",
  "type": "module",
  "files": ["dist"],
  "main": "./dist/my-lib.umd.cjs",
  "module": "./dist/my-lib.js",
  "exports": {
    ".": {
      "import": "./dist/my-lib.js",
      "require": "./dist/my-lib.umd.cjs"
    }
  }
}' > package.json

npm install
npm exec vite build

Expected

Library builds by copying documentation verbatim.

Actual behavior

$ npm exec vite build
failed to load config from /home/user/workspace/myapp/vite.config.js
error during build:
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/user/workspace/myapp/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///home/user/workspace/myapp/vite.config.js?t=1657918746154:6:22
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadConfigFromFile (file:///home/user/workspace/myapp/node_modules/vite/dist/node/chunks/dep-561c5231.js:62653:31)
    at async resolveConfig (file:///home/user/workspace/myapp/node_modules/vite/dist/node/chunks/dep-561c5231.js:62281:28)
    at async doBuild (file:///home/user/workspace/myapp/node_modules/vite/dist/node/chunks/dep-561c5231.js:43272:20)
    at async build (file:///home/user/workspace/myapp/node_modules/vite/dist/node/chunks/dep-561c5231.js:43261:16)
    at async CAC.<anonymous> (file:///home/user/workspace/myapp/node_modules/vite/dist/node/cli.js:747:9)

Problem

Use of __dirname in the provided vite.config.js conflicts with the subsequent recommended package.json which recommends "type": "module".

Proposed solution

Each documentation section should be internally consistent such that a new user can follow the recommendations in that section without getting sidetracked by finding solutions to problems the docs have baked in.

Here the docs should probably do one of the following:

  • Refrain from using __dirname when recommending use of "type": "module" in package.json
  • Don’t recommend use of "type": "module" in package.json when using __dirname in vite.config.js
  • Include a work-around such as those listed here in vite.config.js

Related (but not duplicate) issues

Reproduction

Included in bug description

System Info

Not really relevant, but


$ node --version
v18.5.0


`vite@3.0.0`

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sodateacommented, Jul 16, 2022

🤔Though the bugs are fixed, I think we should refrain from using __dirname with type: module. Not all environments have __dirname polyfilled. It’s not a good practice anyway.

1reaction
bluwycommented, Jul 16, 2022

This should be fixed in https://github.com/vitejs/vite/pull/9121 which isn’t released yet. It will use esbuild to polyfill __dirname so it works here too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common Abbreviations & Acronyms Used In Workers ...
MOD. Modified Duties at Work. MSC. Mandatory Settlement Conference (Conference at WCAB). NCM. Nurse Case Management. NLT. No Lost Time from Work.
Read more >
Public Assistance Program and Policy Guide Version 4 - FEMA
Policy and Guidance Documents Incorporated and Superseded. FEMA Recovery Policy, Public Assistance Donated Resources.
Read more >
Using Chromebooks for Online Student Assessments - Google
Chromebooks are fast, secure, and portable computers that make it easy for students to learn in and out of the classroom. Schools can...
Read more >
Integumentary System – Building a Medical Terminology ...
These slides show cross-sections of the epidermis and dermis of (a) thin and (b) thick skin. Note the significant difference in the thickness...
Read more >
World History: Cultures, States, and Societies to 1500
tion produced stores of food and valuables that could be seized by neighbors. During the 9,000s BCE, set- tlements like Jericho began to...
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