FredKSchott / snowpack

ESM-powered frontend build tool. Instant, lightweight, unbundled development. ✌️
https://www.snowpack.dev
MIT License
19.48k stars 922 forks source link

[BUG] Can't use node imports in ESM config #3097

Closed adamburgess closed 3 years ago

adamburgess commented 3 years ago

Bug Report Quick Checklist

Describe the bug

If I use an ESM config file for snowpack (either type: "module" in package.json OR snowpack.config.mjs), I cannot import node modules.

To Reproduce

https://github.com/adamburgess/snowpack-test-import

  1. npm install --save snowpack
  2. create a snowpack.config.mjs file with content:
    import fs from 'fs'
    export default {};
  3. run snowpack dev
    
    > snowpack dev

[15:21:30] [snowpack] Welcome to Snowpack! Because this is your first time running this project, Snowpack needs to prepare your dependencies. This is a one-time step and the results will be cached for the lifetime of your project. Please wait... [15:21:30] [snowpack] Package "fs" not found. Have you installed it?


### Expected behavior

`fs` should be imported and the build should configure as normal.
Using an equivalent cjs file with standard `require` works fine.

### Anything else?

If I use another module from node_modules it gets compiled (as if it were in used in a client-side script). This seems like a bug too... using a module from a CJS require does not do this.
For example, snowpack.config.js:
```js
import lodash from 'lodash-es'
export default {};
[15:29:38] [snowpack] Welcome to Snowpack! Because this is your first time running
this project, Snowpack needs to prepare your dependencies. This is a one-time step
and the results will be cached for the lifetime of your project. Please wait...
[15:29:38] [snowpack] + lodash-es@4.17.21
[15:29:41] [snowpack] Ready!
...
adamburgess commented 3 years ago

May be a duplicate of #3045, although this issue is specifically for the config files which should be ran in node and nothing else (right? the config files aren't ever used in the browser, yeah?)

adamburgess commented 3 years ago

Verbose output of esm snowpack.config.mjs config:

[15:53:22] [snowpack] Welcome to Snowpack! Because this is your first time running
this project, Snowpack needs to prepare your dependencies. This is a one-time step
and the results will be cached for the lifetime of your project. Please wait...
[15:53:22] [snowpack] Skip scanning README.md for imports (unknown file extension .md)
[15:53:22] [snowpack] Scanning index.html for imports as HTML
[15:53:22] [snowpack] Skip scanning package-lock.json for imports (unknown file extension .json)
[15:53:22] [snowpack] Skip scanning package.json for imports (unknown file extension .json)
[15:53:22] [snowpack] Scanning snowpack.config.mjs for imports as JS
[15:53:22] [snowpack] Package "fs" not found. Have you installed it? 
[15:53:22] [snowpack] Error: Package "fs" not found. Have you installed it? 
    at Object.resolveEntrypoint (/home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:43664:15)
    at Object.resolvePackageImport (/home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:54611:34)
    at /home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:54584:31
    at Array.map (<anonymous>)
    at Object.prepare (/home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:54582:80)
    at async startServer (/home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:131204:9)
    at async Object.command (/home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:131805:9)
    at async cli (/home/adam/WSLProjects/snowpack-test-import/node_modules/snowpack/lib/index.js:132393:9)

Verbose output of cjs snowpack.config.js config:

[15:55:15] [snowpack] Welcome to Snowpack! Because this is your first time running
this project, Snowpack needs to prepare your dependencies. This is a one-time step
and the results will be cached for the lifetime of your project. Please wait...
[15:55:15] [snowpack] Skip scanning README.md for imports (unknown file extension .md)
[15:55:15] [snowpack] Scanning index.html for imports as HTML
[15:55:15] [snowpack] Skip scanning package-lock.json for imports (unknown file extension .json)
[15:55:15] [snowpack] Skip scanning package.json for imports (unknown file extension .json)
[15:55:15] [snowpack] Scanning snowpack.config.js for imports as JS
[15:55:15] [snowpack] No dependencies detected. Ready!
[15:55:15] [snowpack] attaching console.log listeners
[15:55:15] [snowpack] dashboard started
[15:55:15] [snowpack] Mounting directory: '/home/adam/WSLProjects/snowpack-test-import' as URL '/'
[15:55:15] [snowpack] Using in-memory cache: [object Object]
[15:55:15] [snowpack] Server started in 13ms.
adamburgess commented 3 years ago

Ok. If I follow the blank template which adds a mount for the src directory, and then put all my browser code in src, then only src is scanned, and everything works fine.

Maybe user error, I'm not sure. I feel like this shouldn't cause an error. Anyways, closing for now.