ncc icon indicating copy to clipboard operation
ncc copied to clipboard

Cannot use 'import.meta' outside a module on 0.36.0 (not 0.34.0)

Open frbuceta opened this issue 2 years ago • 3 comments

I just updated from version 0.34.0 to 0.36.0. In version 0.34.0 it works perfectly.

ncc: Version 0.36.0
ncc: Compiling file index.js into CJS
...
esm_dirname = (0,external_url_.fileURLToPath)(import.meta.url);
                                                       ^^^^

SyntaxError: Cannot use 'import.meta' outside a module
...

frbuceta avatar Dec 16 '22 11:12 frbuceta

source:

import { fileURLToPath } from "url"

fileURLToPath(`${import.meta.url}/../somestaticfile.txt`)

compile with 0.34.0:

import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";
/******/ /* webpack/runtime/compat */
/******/ 
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/";
/******/ 
/************************************************************************/
var __webpack_exports__ = {};

;// CONCATENATED MODULE: external "url"
const external_url_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("url");
;// CONCATENATED MODULE: ./src/index.js


(0,external_url_namespaceObject.fileURLToPath)(`${"file:///project/home/imyelo/workspace/src/index.js"}/../somestaticfile.txt`);

compile with 0.36.0:

import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";
/******/ /* webpack/runtime/compat */
/******/ 
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/";
/******/ 
/************************************************************************/
var __webpack_exports__ = {};

;// CONCATENATED MODULE: external "url"
const external_url_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("url");
;// CONCATENATED MODULE: ./src/index.js


(0,external_url_namespaceObject.fileURLToPath)(`${import.meta.url}/../somestaticfile.txt`);

seems like webpack doesn't convert import.meta.url staticly in ncc ^0.36.0

reproduces in https://codesandbox.io/p/sandbox/funny-rain-y4qvps

imyelo avatar Mar 10 '23 08:03 imyelo

Thanks for this! I had to downgrade all the way from 0.38 to make this work 🤷🏽‍♂️

schowdhuri avatar Mar 21 '24 15:03 schowdhuri

I just ran into the same issue, and downgrading to 0.34.0 worked for me as well. What a coincidence that we run into the same issue just 8 hours apart, when it's been inactive for a year!

phisch avatar Mar 22 '24 00:03 phisch