Why node throws the error: Cannot find module * imported from * using type="module" in package.json?

Viewed 321

Using svelte-kit generator for the first time today generated a package.json with a "type": "module" in it.

Now I wanna call a postbuild script with:

"scripts": {
  "postbuild": "node ./postbuild/index.js",
}

which contains:

const { tailwindExtractor } = require("tailwindcss/lib/lib/purgeUnusedStyles");
...

Because of the new error require is not defined I changed the ./postbuild/index.js file to this:

import { tailwindExtractor } from "tailwindcss/lib/lib/purgeUnusedStyles";
...

but I'm still getting an error calling that file from node:

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\kit\node_modules\tailwindcss\lib\lib\purgeUnusedStyles' imported from C:\kit\tools\postbuild\index.js
Did you mean to import tailwindcss/lib/lib/purgeUnusedStyles.js?
    at finalizeResolution (internal/modules/esm/resolve.js:276:11)
    at moduleResolve (internal/modules/esm/resolve.js:699:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
    at Loader.resolve (internal/modules/esm/loader.js:86:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
    at link (internal/modules/esm/module_job.js:55:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

What the heck is going on?

I'm using npm 7.8.0 and node v14.16.0.

I tried also with --experimental-modules: I get the same error!

This is the file I need to import: https://github.com/tailwindlabs/tailwindcss/blob/master/src/lib/purgeUnusedStyles.js

0 Answers
Related