vue cli build with target lib: “require is not defined” when component is imported
See original GitHub issueVersion
3.9.1
Reproduction link
https://github.com/louis-sanna/vue-hello-world
Environment info
Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Binaries:
Node: 12.4.0 - /usr/local/bin/node
Yarn: Not Found
npm: 6.10.0 - /usr/local/bin/npm
Browsers:
Chrome: 75.0.3770.100
Firefox: Not Found
Safari: 12.1.1
npmPackages:
@vue/babel-helper-vue-jsx-merge-props: 1.0.0
@vue/babel-plugin-transform-vue-jsx: 1.0.0
@vue/babel-preset-app: 3.9.0
@vue/babel-preset-jsx: 1.0.0
@vue/babel-sugar-functional-vue: 1.0.0
@vue/babel-sugar-inject-h: 1.0.0
@vue/babel-sugar-v-model: 1.0.0
@vue/babel-sugar-v-on: 1.0.0
@vue/cli-overlay: 3.9.0
@vue/cli-plugin-babel: ^3.9.0 => 3.9.0
@vue/cli-plugin-eslint: ^3.9.0 => 3.9.1
@vue/cli-service: ^3.9.0 => 3.9.0
@vue/cli-shared-utils: 3.9.0
@vue/component-compiler-utils: 2.6.0
@vue/preload-webpack-plugin: 1.1.0
@vue/web-component-wrapper: 1.2.0
eslint-plugin-vue: ^5.0.0 => 5.2.3 (4.7.1)
vue: ^2.6.10 => 2.6.10
vue-eslint-parser: 2.0.3 (5.0.0)
vue-hot-reload-api: 2.3.3
vue-loader: 15.7.0
vue-style-loader: 4.1.2
vue-template-compiler: ^2.6.10 => 2.6.10
vue-template-es2015-compiler: 1.9.1
npmGlobalPackages:
@vue/cli: 3.9.1
Steps to reproduce
I’m trying to export a Vue component as a package, and using vue cli to build the dist. However even with a simple hello-world project I can’t make a valid package.
I created a project:
vue create hello-world
Then I modified the package.json:
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --target lib --name vue-hello-world ./src/components/HelloWorld.vue",
"lint": "vue-cli-service lint"
},
"main": "./dist/vue-hello-world.common.js",
Then I call
npm run build
and it compiles without error.
Then I make an import in a vue component (I used a symbolic link in node_modules):
import HelloWorld from "hello-world";
On page render I get the following error:
[Vue warn]: Failed to resolve async component: function MediaViewerPdf() {
return Promise.all(/*! import() */[__webpack_require__.e(62), __webpack_require__.e(46)]).then(__webpack_require__.bind(null, /*! ./viewers/MediaViewerPdf.vue */ "./vuejs/components/mediaViewer/viewers/MediaViewerPdf.vue"));
}
Reason: ReferenceError: require is not defined
What am I missing?
What is expected?
Component should be imported without throwing an error.
What is actually happening?
It does throw an error.
Remarks:
- using
vue inspect
, I checked that in webpack config that:
target: "web"
- I already set resolve.symlinks at false on the importing project.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
vue cli build with target lib: "require is not defined" when ...
vue cli build with target lib : "require is not defined" when component is imported · So you created this component and you...
Read more >[Solved]-vue cli build with target lib: "require is not defined" when ...
Coding example for the question vue cli build with target lib: "require is not defined" when component is imported-Vue.js.
Read more >vite js require is not defined | The AI Search Engine You Control
When importing a react component that imports a CSS file, vite fails with the message: Uncaught ReferenceError: require is not defined. If you...
Read more >How To Fix ReferenceError require is not defined in JavaScript
Your ReferenceError: require is not defined likely has one of two ... In the ES6 syntax you use import rather than require and...
Read more >How to fix the error `require is not defined` in Node.js
It means that the default way of using npm modules is not require anymore, but import . Solution. To get rid of the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We recommend using
NODE_ENV=production
whenever you need to build a project/component and it is the default when running thebuild
command.I don’t know what went wrong on your side but maybe there was a pre-existing
NODE_ENV
environment variable in your shell environment.A quick fix is to change the build script to
vue-cli-service build --target lib --name vue-hello-world ./src/components/HelloWorld.vue --mode production
@sodatea thanks a lot for your help!
You were right I had already
NODE_ENV
set atdevelopment
in my shell.